Part Number Hot Search : 
3050PA 1ZB22 AWL6950R B45197 D401M8 A5800515 1045C 00TTS
Product Description
Full Text Search
 

To Download ATTINY13A-PU Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  features ? high performance, low power avr ? 8-bit microcontroller ? advanced risc architecture ? 120 powerful instructions ? mo st single clock cycle execution ? 32 x 8 general purpose working registers ? fully static operation ? up to 20 mips througput at 20 mhz ? high endurance non-volatile memory segments ? 1k bytes of in-system self-pro grammable flash program memory ? 64 bytes eeprom ? 64 bytes internal sram ? write/erase cycles: 10,000 flash/100,000 eeprom ? data retention: 20 years at 85c/100 years at 25c (see page 6 ) ? programming lock for self-program ming flash & eeprom data security ? peripheral features ? one 8-bit timer/counter with prescaler and two pwm channels ? 4-channel, 10-bit adc with internal voltage reference ? programmable watchdog timer wi th separate on-chip oscillator ? on-chip analog comparator ? special microcontroller features ? debugwire on-chip debug system ? in-system programmable via spi port ? external and internal interrupt sources ? low power idle, adc noise redu ction, and powe r-down modes ? enhanced power-on reset circuit ? programmable brown-out detection circuit with software disable function ? internal calibrated oscillator ? i/o and packages ? 8-pin pdip/soic: six programmable i/o lines ? 10-pad mlf: six programmable i/o lines ? 20-pad mlf: six programmable i/o lines ? operating voltage: ? 1.8 ? 5.5v ? speed grade: ? 0 ? 4 mhz @ 1.8 ? 5.5v ? 0 ? 10 mhz @ 2.7 ? 5.5v ? 0 ? 20 mhz @ 4.5 ? 5.5v ? industrial temperature range ? low power consumption ? active mode: ? 190 a at 1.8 v and 1 mhz ?idle mode: ? 24 a at 1.8 v and 1 mhz 8-bit microcontroller with 1k bytes in-system programmable flash attiny13a rev. 8126d?avr?11/09
2 8126d?avr?11/09 attiny13a 1. pin configurations figure 1-1. pinout of attiny13a 1 2 3 4 8 7 6 5 (pcint5/reset/adc0/dw) pb5 (pcint3/clki/adc3) pb3 (pcint4/adc2) pb4 gnd vcc pb2 (sck/adc1/t0/pcint2) pb1 (miso/ain1/oc0b/int0/pcint1) pb0 (mosi/ain0/oc0a/pcint0) 8-pdip/soic 1 2 3 4 5 20-qfn/mlf 15 14 13 12 11 20 19 18 17 16 6 7 8 9 10 (pcint5/reset/adc0/dw) pb5 (pcint3/clki/adc3) pb3 dnc dnc (pcint4/adc2) pb4 dnc dnc gnd dnc dnc vcc pb2 (sck/adc1/t0/pcint2) dnc pb1 (miso/ain1/oc0b/int0/pcint1) pb0 (mosi/ain0/oc0a/pcint0) dnc dnc dnc dnc dnc note: bottom pad should be soldered to ground. dnc: do not connect 1 2 3 4 5 10-qfn/mlf 10 9 8 7 6 (pcint5/reset/adc0/dw) pb5 (pcint3/clki/adc3) pb3 dnc (pcint4/adc2) pb4 gnd vcc pb2 (sck/adc1/t0/pcint2) dnc pb1 (miso/ain1/oc0b/int0/pcint1) pb0 (mosi/ain0/oc0a/pcint0) note: bottom pad should be soldered to ground. dnc: do not connect
3 8126d?avr?11/09 attiny13a 1.1 pin description 1.1.1 vcc supply voltage. 1.1.2 gnd ground. 1.1.3 port b (pb5:pb0) port b is a 6-bit bi-directional i/o port with inte rnal pull-up resistors (selected for each bit). the port b output buffers have symmetrical drive characteristics with both high sink and source capability. as inputs, port b pins that are externally pulled low will source current if the pull-up resistors are activated. the port b pins are tri-stated when a reset condition becomes active, even if the clock is not running. port b also serves the functions of various special features of the attiny13a as listed on page 55 . 1.1.4 reset reset input. a low level on this pin for longer than the minimum pulse length will generate a reset, even if the clock is not running and prov ided the reset pin has not been disabled. the min- imum pulse length is given in table 18-4 on page 120 . shorter pulses are not guaranteed to generate a reset. the reset pin can also be used as a (weak) i/o pin.
4 8126d?avr?11/09 attiny13a 2. overview the attiny13a is a low-power cmos 8-bit microcontroller based on the avr enhanced risc architecture. by executing powerfu l instructions in a single cloc k cycle, the at tiny13a achieves throughputs approaching 1 mips per mhz allowing the system designer to optimize power con- sumption versus processing speed. 2.1 block diagram figure 2-1. block diagram program counter internal o s cilla tor watchdog timer s tack pointer program fla s h s ram mcu control regi s ter general purpo s e regi s ter s in s truction regi s ter timer/ counter0 in s truction decoder data dir. reg.port b data regi s ter port b programming logic timing and control mcu s tatu s regi s ter s tatu s regi s ter alu port b driver s pb0-pb5 vcc gnd control line s 8 -bit databu s z adc / analog comparator interrupt unit calibrated y x re s et clki watchdog o s cillator data eeprom
5 8126d?avr?11/09 attiny13a the avr core combines a rich instruction set with 32 general purpose working registers. all 32 registers are directly connected to the arit hmetic logic unit (alu), allowing two independent registers to be accessed in one single instruction executed in one clock cycle. the resulting architecture is more code efficient while achiev ing throughputs up to ten times faster than con- ventional cisc microcontrollers. the attiny13a provides the following features: 1k byte of in-system programmable flash, 64 bytes eeprom, 64 bytes sram, 6 ge neral purpose i/o lines, 32 general purpose working reg- isters, one 8-bit timer/counter with compare modes, internal and external interrupts, a 4- channel, 10-bit adc, a programmable watchdog ti mer with internal oscillator, and three soft- ware selectable power saving modes. the idle mode stops the cpu while allowing the sram, timer/counter, adc, analog comparator, and interrupt system to continue functioning. the power-down mode saves the register contents, di sabling all chip functi ons until the next inter- rupt or hardware reset. the adc noise reduction mode stops the cpu and all i/o modules except adc, to minimize switch ing noise during adc conversions. the device is manufactured using atmel?s high density non-volatile memory technology. the on-chip isp flash allows the pr ogram memory to be re-programmed in-system through an spi serial interface, by a conventi onal non-volatile memory programmer or by an on-chip boot code running on the avr core. the attiny13a avr is supported with a full suite of program and syst em development tools including: c compilers, macro assemblers, program debugger/si mulators, and evaluation kits.
6 8126d?avr?11/09 attiny13a 3. about 3.1 resources a comprehensive set of drivers, application notes, data sheets and descriptions on development tools are available for downloa d at http://www.a tmel.com/avr. 3.2 code examples this documentation contains simple code examples that briefly show how to use various parts of the device. these code examples assume that the part specific header file is included before compilation. be aware that not all c compiler vendors include bit definitions in the header files and interrupt handling in c is compiler dependent. please confirm with the c compiler documen- tation for more details. 3.3 data retention reliability qualification results sh ow that the projected data retention failure rate is much less than 1 ppm over 20 years at 85c or 100 years at 25 ? c.
7 8126d?avr?11/09 attiny13a 4. cpu core this section discusses the avr core architecture in general. the main function of the cpu core is to ensure correct program execution. the cpu must therefore be able to access memories, perform calculations, control peri pherals, and handle interrupts. 4.1 architectural overview figure 4-1. block diagram of the avr architecture in order to maximize performance and parallelism, the avr uses a harvard architecture ? with separate memories and buses for program and data. instructions in the program memory are executed with a single level pipe lining. while one instruction is being executed, the next instruc- tion is pre-fetched from the prog ram memory. this concept enables instructions to be executed in every clock cycle. the program memory is in-system reprogrammable flash memory. flash program memory instruction register instruction decoder program counter control lines 32 x 8 general purpose registrers alu status and control i/o lines eeprom data bus 8-bit data sram direct addressing indirect addressing interrupt unit watchdog timer analog comparator i/o module 2 i/o module1 i/o module n
8 8126d?avr?11/09 attiny13a the fast-access register file contains 32 x 8-bit general purpose working registers with a single clock cycle access time. this allows single-cycle ar ithmetic logic unit (alu ) operation. in a typ- ical alu operation, two operands are output from the register file, the operation is executed, and the result is stored back in the register file ? in one clock cycle. six of the 32 registers can be used as three 16 -bit indirect address register pointers for data space addressing ? enabling efficient address calculations. one of the these address pointers can also be used as an address pointer for look up tables in flash program memory. these added function registers are the 16-bit x-, y-, and z-register, described later in this section. the alu supports arithmetic and logic operations between registers or between a constant and a register. single register operations can also be executed in the alu. after an arithmetic opera- tion, the status register is up dated to reflect information about the result of the operation. program flow is provided by conditional and uncon ditional jump and call instructions, able to directly address the whole addres s space. most avr instructions have a single 16-bit word for- mat. every program memory address c ontains a 16- or 32-bit instruction. during interrupts and subroutine calls, the return address program counter (pc) is stored on the stack. the stack is effectively allocated in th e general data sram, and consequently the stack size is only limited by the total sram size an d the usage of the sram. all user programs must initialize the sp in the reset routine (before subroutines or interrupts are executed). the stack pointer (sp) is read/write accessible in the i/o space. the data sram can easily be accessed through the five different addressing mo des supported in the avr architecture. the memory spaces in the avr architecture are all linear and regular memory maps. a flexible interrupt module has its control r egisters in the i/o spac e with an additional global interrupt enable bit in the status register. all interrupts have a s eparate interrupt vector in the interrupt vector table. the interrupts have priority in accordance with their interrupt vector posi- tion. the lower the interr upt vector address, the higher the priority. the i/o memory space contains 64 addresses for cpu peripheral functi ons as control regis- ters, spi, and other i/o functions. the i/o memory can be accessed directly, or as the data space locations following those of the register file, 0x20 - 0x5f. 4.2 alu ? arithm etic logic unit the high-performance avr alu operates in dire ct connection with all the 32 general purpose working registers. within a single clock cycle, arithmetic operations between general purpose registers or between a register and an immedi ate are executed. the alu operations are divided into three main categories ? arithmetic, logical, and bit-functions. some implementations of the architecture also provide a powerful multip lier supporting both signed/unsigned multiplication and fractional format. see the ?instruction set? section for a detailed description. 4.3 status register the status register contains information about th e result of the most recently executed arithme- tic instruction. this information can be used for altering program flow in order to perform conditional operations. note that the status re gister is updated after all alu operations, as specified in the instructio n set reference. this will in many cases remove the need for using the dedicated compare instructions, resulting in faster and more compact code. the status register is not automatically st ored when entering an interrupt routine and restored when returning from an interrupt. this must be handled by software.
9 8126d?avr?11/09 attiny13a 4.3.1 sreg ? status register ? bit 7 ? i: global interrupt enable the global interrupt enable bit must be set for th e interrupts to be enabled. the individual inter- rupt enable control is then performed in separate control registers. if the global interrupt enable register is cleared, none of t he interrupts are enabled independent of the individual interrupt enable settings. the i-bit is cleared by hardware after an interrupt has occurred, and is set by the reti instruction to enable subsequent interr upts. the i-bit can also be set and cleared by the application with the sei and cli instructions, as described in the instructio n set reference. ? bit 6 ? t: bit copy storage the bit copy instructions bld (bit load) and bst (b it store) use the t-bi t as source or desti- nation for the operated bit. a bit from a register in the register file can be copied into t by the bst instruction, and a bit in t can be copied into a bit in a register in the register file by the bld instruction. ? bit 5 ? h: half carry flag the half carry flag h indicates a half carry in some arithmetic operation s. half carry is useful in bcd arithmetic. see the ?instruction set description? for detailed information. ? bit 4 ? s: sign bit, s = n v the s-bit is always an exclusive or between the negative flag n and the two?s complement overflow flag v. see the ?instruction set description? for detailed information. ? bit 3 ? v: two?s complement overflow flag the two?s complement overflow flag v suppor ts two?s complement arithmetics. see the ?instruction set description? for detailed information. ? bit 2 ? n: negative flag the negative flag n indicates a negative result in an arithmetic or logic operation. see the ?instruction set description? for detailed information. ? bit 1 ? z: zero flag the zero flag z indicates a zero result in an arithmetic or logic operation. see the ?instruction set description? for detailed information. ? bit 0 ? c: carry flag the carry flag c indicates a carry in an arithmetic or logic operation. see the ?instruction set description? for de tailed information. bit 76543210 0x3f i t h s v n z c sreg read/write r/w r/w r/ wr/wr/wr/wr/wr/w initial value 0 0 0 0 0 0 0 0
10 8126d?avr?11/09 attiny13a 4.4 general purpose register file the register file is optimized for the avr enhanc ed risc instruction set. in order to achieve the required performance and flex ibility, the following in put/output schemes ar e supported by the register file: ? one 8-bit output operand and one 8-bit result input ? two 8-bit output operands and one 8-bit result input ? two 8-bit output operands and one 16-bit result input ? one 16-bit output operand and one 16-bit result input figure 4-2 shows the structure of the 32 genera l purpose working registers in the cpu. figure 4-2. avr cpu general purpose working registers most of the instructions operating on the register file have direct access to all registers, and most of them are single cycle instructions. as shown in figure 4-2 on page 10 , each register is also assigned a data memory address, mapping them directly into the first 32 location s of the user data s pace. although not being physically implemented as sram locations, this memory organiza tion provides great flexibility in access of the registers, as the x-, y- and z-poi nter registers can be set to index any register in the file. 4.4.1 the x-register, y-register, and z-register the registers r26..r31 have some added functi ons to their general purpose usage. these reg- isters are 16-bit address pointers for indirect addressing of the data space. the three indirect address registers x, y, and z are defined as described in figure 4-3 on page 11 . 7 0 addr. r0 0x00 r1 0x01 r2 0x02 ? r13 0x0d general r14 0x0e purpose r15 0x0f working r16 0x10 registers r17 0x11 ? r26 0x1a x-register low byte r27 0x1b x-register high byte r28 0x1c y-register low byte r29 0x1d y-register high byte r30 0x1e z-register low byte r31 0x1f z-register high byte
11 8126d?avr?11/09 attiny13a figure 4-3. the x-, y-, and z-registers in the different addressing modes these address registers have functions as fixed displacement, automatic increment, and automatic decrement (s ee the instruction set reference for details). 4.5 stack pointer the stack is mainly used for storing temporary data, for storing local variables and for storing return addresses after interrupts and subroutine calls. the stack pointer register always points to the top of the stack. note that the stack is implemented as growing from higher memory loca- tions to lower memory location s. this implies that a stack push command decreases the stack pointer. the stack pointer points to the data sram stack area where the subroutine and interrupt stacks are located. this stack space in the data sram is automati call defined to the last address in sram during power on reset. the stack pointer must be set to point above 0x60. the stack pointer is decremented by one w hen data is pushed onto the stack with the push instruction, and it is decremen ted by two when the return address is pushed onto the stack with subroutine call or interrupt. the stack pointer is incremented by one when data is popped from the stack with the pop instruction, and it is incremented by two when data is popped from the stack with return from subroutine re t or return from interrupt reti. the avr stack pointer is implemented as two 8- bit registers in the i/o space. the number of bits actually used is implementation dependent. no te that the data space in some implementa- tions of the avr architecture is so small that only spl is needed. in this case, the sph register will not be present. 4.5.1 spl ? stack pointer low 15 xh xl 0 x-register 707 0 r27 (0x1b) r26 (0x1a) 15 yh yl 0 y-register 707 0 r29 (0x1d) r28 (0x1c) 15 zh zl 0 z-register 7070 r31 (0x1f) r30 (0x1e) bit 76543210 0x3d sp7 sp6 sp5 sp4 sp3 sp2 sp1 sp0 spl read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value10011111
12 8126d?avr?11/09 attiny13a 4.6 instruction execution timing this section describes the general access timing concepts for instructi on execution. the avr cpu is driven by the cpu clock clk cpu , directly generated from the selected clock source for the chip. no internal clo ck division is used. figure 4-4 on page 12 shows the parallel instruction fetches and instruction executions enabled by the harvard architecture and the fast access re gister file concept. th is is the basic pipelin- ing concept to obtain up to 1 mips per mhz with the corresponding unique results for functions per cost, functions per clocks, and functions per power-unit. figure 4-4. the parallel instruction fetche s and instruction executions figure 4-5 on page 12 shows the internal timing concept for the register file. in a single clock cycle an alu operation using two register operands is executed, and the re sult is stored back to the destination register. figure 4-5. single cycle alu operation 4.7 reset and in terrupt handling the avr provides several different interrupt sources. these interrupts and the separate reset vector each have a separate program vector in the program memory space. all interrupts are assigned individual enable bits which must be writ ten logic one together with the global interrupt enable bit in the status register in order to enable the interrupt. the lowest addresses in the program memory space are by default defined as the reset and interrupt vectors. the complete list of vectors is shown in ?interrupts? on page 45 . the list also determines the priority levels of the different interrupts. the lower the address the higher is the clk 1st instruction fetch 1st instruction execute 2nd instruction fetch 2nd instruction execute 3rd instruction fetch 3rd instruction execute 4th instruction fetch t1 t2 t3 t4 cpu total execution time register operands fetch alu operation execute result write back t1 t2 t3 t4 clk cpu
13 8126d?avr?11/09 attiny13a priority level. reset has the highest priority, and next is int0 ? the external interrupt request 0. when an interrupt occurs, the global interrupt enable i-bit is cleared and all interrupts are dis- abled. the user software can write logic one to the i-bit to enable nested interrupts. all enabled interrupts can then interrupt the current interrupt routine. the i-bit is automatically set when a return from interrupt instru ction ? reti ? is executed. there are basically two types of interrupts. the fi rst type is triggered by an event that sets the interrupt flag. for these interrupts, the program co unter is vectored to th e actual interrupt vec- tor in order to execute the interrupt handli ng routine, and hardware clears the corresponding interrupt flag. interrupt flags can also be cleared by writing a logic one to the flag bit position(s) to be cleared. if an interrupt condition occurs while the corresponding interrupt enable bit is cleared, the interrupt fl ag will be set and remember ed until the interrupt is enabled, or the flag is cleared by software. similarly, if one or more interrupt conditions occur while the global interrupt enable bit is cleared, the corr esponding interrupt flag(s) will be set and remembered until the global interrupt enable bit is set, and will then be exec uted by order of priority. the second type of interrupts will trigger as long as the interrupt condition is present. these interrupts do not necessarily have interrupt flags. if the interrupt condition disappears before the interrupt is enabled, the in terrupt will not be triggered. when the avr exits from an inte rrupt, it will always retu rn to the main pr ogram and execute one more instruction before any pending interrupt is served. note that the status register is not automatically stored when entering an interrupt routine, nor restored when returning from an interrupt routine. this must be handled by software. when using the cli instruction to disable interrupts, the interrup ts will be immediately disabled. no interrupt will be ex ecuted after the cli instru ction, even if it occurs simultaneously with the cli instruction. the following example shows how th is can be used to avoid interrupts during the timed eeprom write sequence.. note: see ?code examples? on page 6 . assembly code example in r16, sreg ; store sreg value cli ; disable interrupts during timed sequence sbi eecr, eempe ; start eeprom write sbi eecr, eepe out sreg, r16 ; restore sreg value (i-bit) c code example char csreg; csreg = sreg; /* store sreg value */ /* disable interrupts during timed sequence */ __disable_interrupt(); eecr |= (1< 14 8126d?avr?11/09 attiny13a when using the sei instruction to enable interr upts, the instruction following sei will be exe- cuted before any pending interrupts, as shown in this example. note: see ?code examples? on page 6 . 4.7.1 interrupt response time the interrupt execution response for all the enabled avr interrupts is four clock cycles mini- mum. after four clock cycles the program vector address for the actual interrupt handling routine is executed. during this four clock cycle perio d, the program counter is pushed onto the stack. the vector is normally a jump to the interrupt routine, and this jump takes three clock cycles. if an interrupt occurs during execution of a multi- cycle instruction, this in struction is completed before the interrupt is served. if an interrupt occurs when the mcu is in sleep mode, the interrupt execution response time is increased by four clo ck cycles. this increase co mes in addition to the start-up time from the selected sleep mode. a return from an interrupt handling routine take s four clock cycles. during these four clock cycles, the program counter (two bytes) is po pped back from the stack, the stack pointer is incremented by two, and the i-bit in sreg is set. assembly code example sei ; set global interrupt enable sleep ; enter sleep, waiting for interrupt ; note: will enter sleep before any pending ; interrupt(s) c code example __enable_interrupt(); /* set global interrupt enable */ __sleep(); /* enter sleep, waiting for interrupt */ /* note: will enter sleep before any pending interrupt(s) */
15 8126d?avr?11/09 attiny13a 5. memories this section describes the different memories in the attiny13a. the avr architecture has two main memory spaces, the data memory and t he program memory space. in addition, the attiny13a features an eeprom memory for data storage. all three memory spaces are linear and regular. 5.1 in-system reprogrammabl e flash program memory the attiny13a contains 1k byte on-chip in -system reprogrammable flash memory for pro- gram storage. since all avr instructions are 16 or 32 bits wide, the flas h is organized as 512 x 16. the flash memory has an endurance of at leas t 10,000 write/erase cycles. the attiny13a pro- gram counter (pc) is nine bits wide, thus addressing the 512 program memory locations. ?memory programming? on page 103 contains a detailed description on flash data serial down- loading using the spi pins. constant tables can be allocated within the entire program memory address space (see the lpm ? load program memory instruction description). timing diagrams for instruction fetc h and execution are presented in ?instruction execution tim- ing? on page 12 . figure 5-1. program memory map 5.2 sram data memory figure 5-2 on page 16 shows how the attiny13a sram memory is organized. the lower 160 data memory loca tions address both the register file, the i/o me mory and the internal data sram. the first 32 locations address the register file, the next 64 locations the standard i/o memory, and the last 64 locations address the internal data sram. the five different addressing modes for the data memory cover: direct, indirect with displace- ment, indirect, indirect with pre-decrement, and indirect with post-increment. in the register file, registers r26 to r31 feature the indirect addressing pointer registers. the direct addressing reaches the entire data space. the indirect with displacement mode reaches 63 address locations from the base address given by the y- or z-register. 0x0000 0x01ff program memory
16 8126d?avr?11/09 attiny13a when using register indirect addressing modes with automatic pre-decrement and post-incre- ment, the address registers x, y, and z are decremented or incremented. the 32 general purpose working registers, 64 i/o registers, and the 64 bytes of internal data sram in the attiny13a are all accessible through all these addressing modes. the register file is described in ?general purpose register file? on page 10 . figure 5-2. data memory map 5.2.1 data memory access times this section describes the general access timi ng concepts for internal memory access. the internal data sram access is performed in two clk cpu cycles as described in figure 5-3 . figure 5-3. on-chip data sram access cycles 5.3 eeprom data memory the attiny13a contains 64 byte s of data eeprom memory. it is organized as a separate data space, in which single bytes can be read and written. the eeprom has an endurance of at least 100,000 write/erase cycles. the access between the eeprom and the cpu is described in the following, specif ying the eeprom address registers, the eeprom data register, and the eeprom control register. for a detailed description of serial data downloading to the eeprom, see page 106 . 3 2 regi s ter s 64 i/o regi s ter s intern a l s ram (64 x 8 ) 0x0000 - 0x001f 0x0020 - 0x005f 0x00 9 f 0x0060 data memory clk wr rd data data address address valid t1 t2 t3 compute address read write cpu memory access instruction next instruction
17 8126d?avr?11/09 attiny13a 5.3.1 eeprom read/write access the eeprom access registers are accessible in the i/o space. the write access times for the eeprom are given in table 5-1 on page 21 . a self-timing func- tion, however, lets the user software detect w hen the next byte can be written. if the user code contains instructions that writ e the eeprom, some precautions mu st be taken. in heavily fil- tered power supplies, v cc is likely to rise or fall slo wly on power-up/down. this causes the device for some period of time to run at a volt age lower than specified as minimum for the clock frequency used. see ?preventing eeprom corruption? on page 19 for details on how to avoid problems in these situations. in order to prevent unintentional eeprom writes, a specific write procedure must be followed. refer to ?atomic byte programming? on page 17 and ?split byte programming? on page 17 for details on this. when the eeprom is read, the cpu is halted for fo ur clock cycles before the next in struction is executed. when the eeprom is written, the cpu is halte d for two clock cycles before the next instruction is executed. 5.3.2 atomic byte programming using atomic byte programming is the simplest mode. when writing a by te to the eeprom, the user must write the address in to the eearl register and data into eedr register. if the eepmn bits are zero, writing eepe (within four cycles after eempe is written) will trigger the erase/write operation. both the erase and writ e cycle are done in one operation and the total programming time is given in table 5-1 on page 21 . the eepe bit remains set until the erase and write operations are completed. while the devi ce is busy with progra mming, it is not possi- ble to do any other eeprom operations. 5.3.3 split byte programming it is possible to split the erase and write cycle in two different operations. this may be useful if the system requires short access time for some limited period of ti me (typically if the power sup- ply voltage falls). in order to take advantage of th is method, it is required that the locations to be written have been erased before the write oper ation. but since the erase and write operations are split, it is possible to do the erase operations when the system allows doing time-critical operations (typically after power-up). 5.3.4 erase to erase a byte, t he address must be written to eearl. if the eepmn bits are 0b01, writing the eepe (within four cycles after eempe is written) will trigger the er ase operation on ly (program- ming time is given in table 5-1 on page 21 ). the eepe bit remains se t until the erase operation completes. while the device is busy programming, it is not possible to do any other eeprom operations. 5.3.5 write to write a location, the user must write the address into eearl and the data into eedr. if the eepmn bits are 0b10, writing the eepe (within four cycles after eempe is written) will trigger the write operation only (programming time is given in table 5-1 on page 21 ). the eepe bit remains set until the write operat ion completes. if the location to be written has not been erased before write, the data that is stored must be considered as lost. while the device is busy with programming, it is not possible to do any ot her eeprom operations.
18 8126d?avr?11/09 attiny13a the calibrated oscillator is used to time the eeprom accesses. make sure the oscillator fre- quency is within the requirements described in ?osccal ? oscillator ca libration register? on page 27 . the following code examples show one assembly and one c function for erase, write, or atomic write of the eeprom. the examples assume that interrupts are controlled (e.g., by disabling interrupts globally) so that no interrupts will occur during ex ecution of th ese functions. note: see ?code examples? on page 6 . assembly code example eeprom_write: ; wait for completion of previous write sbic eecr,eepe rjmp eeprom_write ; set programming mode ldi r16, (0<>eepm0) /* set up address and data registers */ eearl = ucaddress; eedr = ucdata; /* write logical one to eempe */ eecr |= (1< 19 8126d?avr?11/09 attiny13a the next code examples show assembly and c functions for reading the eeprom. the exam- ples assume that interrupts are controlled so that no interrupts will occur during execution of these functions. note: see ?code examples? on page 6 . 5.3.6 preventing eepr om corruption during periods of low v cc , the eeprom data can be corrupted because the supp ly voltage is too low for the cpu and the eeprom to operate properly. these issues are the same as for board level systems using eepr om, and the same design so lutions should be applied. an eeprom data corruption can be caused by two situations when the volt age is too low. first, a regular write sequence to the eeprom requires a minimum voltage to operate correctly. sec- ondly, the cpu itself can execut e instructions incorr ectly, if the supply voltage is too low. eeprom data corruption can easily be avoided by follo wing this design recommendation: keep the avr reset active (low) during periods of insufficient power supply voltage. this can be done by enabling the internal brown-out detect or (bod). if the detection level of the internal bod does not match the needed detection level, an external low v cc reset protection circuit can be used. if a reset occurs while a write operation is in progress , the write operation will be com- pleted provided that the power supply voltage is sufficient. assembly code example eeprom_read: ; wait for completion of previous write sbic eecr,eepe rjmp eeprom_read ; set up address (r17) in address register out eearl, r17 ; start eeprom read by writing eere sbi eecr,eere ; read data from data register in r16,eedr ret c code example unsigned char eeprom_read( unsigned char ucaddress) { /* wait for completion of previous write */ while(eecr & (1< 20 8126d?avr?11/09 attiny13a 5.4 i/o memory the i/o space definition of the attiny13a is shown in ?register summary? on page 155 . all attiny13a i/os and peripherals are placed in the i/o space. all i/o locations may be accessed by the ld/lds/ldd and st/sts/std instructions, transferring data between the 32 general purpose working registers and the i/o space. i/o registers wi thin the address range 0x00 - 0x1f are directly bit-acce ssible using the sbi and cbi instru ctions. in these registers, the value of single bits can be checked by using the sbis and sbic instructions. refer to the instruction set section for more details. when us ing the i/o specific commands in and out, the i/o addresses 0x00 - 0x3f must be used. when addr essing i/o registers as data space using ld and st instructions, 0x20 must be added to these addresses. for compatibility with future devices, reserved bits should be written to zero if accessed. reserved i/o memory addresses should never be written. some of the status flags are cl eared by writing a logical one to them. note that, unlike most other avrs, the cbi and sbi instructions will only operate on the specified bit, and can therefore be used on registers containing such status flags. the cbi and sbi instructions work with reg- isters 0x00 to 0x1f only. the i/o and peripherals control registers are explained in later sections. 5.5 register description 5.5.1 eearl ? eeprom address register ? bits 7:6 ? res: reserved bits these bits are reserved bits in the attiny13a and will always read as zero. ? bits 5:0 ? eear[5:0]: eeprom address the eeprom address register ? eearl ? s pecifies the eeprom address in the 64 bytes eeprom space. the eeprom data bytes are addr essed linearly between 0 and 63. the initial value of eearl is undefined. a proper value must be written before the eeprom may be accessed. 5.5.2 eedr ? eeprom data register ? bits 7:0 ? eedr7:0: eeprom data for the eeprom write operation the eedr regist er contains the data to be written to the eeprom in the address given by the eearl re gister. for the eeprom read operation, the eedr contains the data read out from the eeprom at the add ress given by eearl. bit 76543210 0x1e ? ? eear5 eear4 eear3 eear2 eear1 eear0 eearl read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 x x x x x x bit 76543210 0x1d eedr7 eedr6 eedr5 eedr4 eedr3 eedr2 eedr1 eedr0 eedr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value x x x x x x x x
21 8126d?avr?11/09 attiny13a 5.5.3 eecr ? eeprom control register ? bit 7 ? res: reserved bit this bit is reserved for future use and will alwa ys read as 0 in attiny13 a. for compatibility with future avr devices, always wr ite this bit to zero. after reading, mask out this bit. ? bit 6 ? res: reserved bit this bit is reserved in the atti ny13a and will always read as zero. ? bits 5:4 ? eepm[1:0]: eeprom programming mode bits the eeprom programming mode bits setting def ines which programming action that will be triggered when writing eepe. it is possible to program data in one atomic operation (erase the old value and program the new value) or to split the erase and write operations in two different operations. the programming times for the different modes are shown in table 5-1 on page 21 . while eepe is set, any write to eepmn will be ignored. during reset, the eepmn bits will be reset to 0b00 unless the eeprom is busy programming. ? bit 3 ? eerie: eeprom ready interrupt enable writing eerie to one enables the eeprom ready interrupt if the i-bit in sreg is set. writing eerie to zero disables the interrupt. the eep rom ready interrupt gener ates a constant inter- rupt when non-volatile memory is ready for programming. ? bit 2 ? eempe: eeprom master program enable the eempe bit determines whether writing eepe to o ne will have effect or not. when eempe is set, setting eepe within four cl ock cycles will program the eeprom at the selected address. if eempe is zero, setting eepe will have no effect. when eempe has been written to one by software, hardware clears the bit to zero after four clock cycles. ? bit 1 ? eepe: eeprom program enable the eeprom program enable signal eepe is th e programming enable signal to the eeprom. when eepe is written, the ee prom will be programmed accord ing to the eepmn bits setting. the eempe bit must be written to one before a logical one is written to eepe, otherwise no eeprom write takes place. when the write access time has elapsed, the eepe bi t is cleared by hardware. wh en eepe has been set, the cpu is halted for two cycles be fore the next instruction is executed. bit 76543210 0x1c ? ? eepm1 eepm0 eerie eempe eepe eere eecr read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 x x 0 0 x 0 table 5-1. eeprom mode bits eepm1 eepm0 programming time operation 0 0 3.4 ms erase and write in one operation (atomic operation) 0 1 1.8 ms erase only 1 0 1.8 ms write only 1 1 ? reserved for future use
22 8126d?avr?11/09 attiny13a ? bit 0 ? eere: eeprom read enable the eeprom read enable signal ? eere ? is t he read strobe to the eeprom. when the cor- rect address is set up in the eearl register, t he eere bit must be writte n to one to trigger the eeprom read. the eeprom read access takes one instruction, and the requ ested data is available immediately. when t he eeprom is read, the cpu is ha lted for four cycles before the next instruction is executed. th e user should poll the eepe bit be fore starting the read opera- tion. if a write operation is in progress, it is neither possible to read the eeprom, nor to change the eearl register.
23 8126d?avr?11/09 attiny13a 6. system clock and clock options 6.1 clock systems and their distribution figure 6-1 presents the principal clock systems in the avr and their distribution. all of the clocks need not be active at a given time. in order to reduce power consumption, the clocks to modules not being used can be halted by using different sleep modes, as described in ?power manage- ment and sleep modes? on page 30 . the clock systems are detailed below. figure 6-1. clock distribution 6.1.1 cpu clock ? clk cpu the cpu clock is routed to parts of the syst em concerned with operat ion of the avr core. examples of such modules are the general pur pose register file, the status register and the data memory holding the stack po inter. halting the cpu clock inhibits the core from performing general operations and calculations. 6.1.2 i/o clock ? clk i/o the i/o clock is used by the majority of the i/o modules, like timer/counter. the i/o clock is also used by the external interrupt module, but note that some external interrupts are detected by asynchronous logic, allowing such interrupts to be detected even if the i/o clock is halted. 6.1.3 flash clock ? clk flash the flash clock controls operation of the flash interface. the flash clock is usually active simul- taneously with the cpu clock. general i/o modules cpu core ram clk i/o avr clock control unit clk cpu flash and eeprom clk flash source clock watchdog timer watchdog oscillator reset logic clock multiplexer watchdog clock calibrated rc oscillator external clock adc clk adc
24 8126d?avr?11/09 attiny13a 6.1.4 adc clock ? clk adc the adc is provided with a dedicated clock domain. this allows halting the cpu and i/o clocks in order to reduce noise generated by digital circ uitry. this gives more accurate adc conversion results. 6.2 clock sources the device has the following clock source opti ons, selectable by flash fuse bits as shown below. the clock from the selected source is inpu t to the avr clock generator, and routed to the appropriate modules. note: 1. for all fuses ?1? means unprogrammed while ?0? means programmed. the various choices for each clocking option is given in the following sections. when the cpu wakes up from power-down or power-save, the sele cted clock source is used to time the start- up, ensuring stable osc illator operation bef ore instruction execution st arts. when the cpu starts from reset, there is an additional delay allowi ng the power to reach a stable level before com- mencing normal operation. the watchdog oscillator is used for timing this real-time part of the start-up time. the number of wdt oscillator cycles used for each time-out is shown in table 6- 2 . 6.2.1 external clock to drive the device from an external cloc k source, clki should be driven as shown in figure 6- 2 . to run the device on an ex ternal clock, the cksel fuses must be programmed to ?00?. figure 6-2. external clock drive configuration table 6-1. device clocking options select device clocking option cksel1:0 (1) external clock (see page 24 )00 calibrated internal 4.8/9.6 mhz oscillator (see page 25 )01, 10 internal 128 khz oscillator (see page 26 )11 table 6-2. number of watchdog oscillator cycles typ time-out number of cycles 4 ms 512 64 ms 8k (8,192) external clock s ignal clki gnd
25 8126d?avr?11/09 attiny13a when this clock source is selected, start-up times are determined by the sut fuses as shown in table 6-3 . when applying an external clock, it is required to avoid sudden changes in the applied clock fre- quency to ensure stable operation of the mcu. a variation in frequency of more than 2% from one clock cycle to the next can lead to unpredictable behavior. it is required to en sure that the mcu is kept in reset during such changes in the clock frequency. note that the system clock prescaler can be used to implement run-time changes of the internal clock frequency while still ensuri ng stable operation. refer to ?system clock prescaler? on page 26 for details. 6.2.2 calibrated internal 4.8/9.6 mhz oscillator the calibrated internal oscillator provides a 4.8 or 9.6 mhz clock source. the frequency is nomi- nal at 3v and 25 ? c. if the frequency exceeds the specification of the device (depends on v cc ), the ckdiv8 fuse must be programmed so that the internal clock is divided by 8 during start-up. see ?system clock prescaler? on page 26. for more details. the internal oscillator is selected as the system clock by programming the cksel fuses as shown in table 6-4 . if selected, it will operate with no external components. note: 1. the device is shipped with this option selected. during reset, hardware loads the calibration dat a into the osccal r egister and thereby auto- matically calibrates the oscillator. there ar e separate calibration bytes for 4.8 and 9.6 mhz operation but only one is automatically loaded during reset (see section ?calibration bytes? on page 105 ). this is because the only difference bet ween 4.8 mhz and 9.6 mhz mode is an inter- nal clock divider. by changing the osccal register from sw, see ?osccal ? oscillator ca libration register? on page 27 , it is possible to get a higher calibration ac curacy than by using the factory calibration. see ?calibrated internal rc o scillator accuracy? on page 119 . when this oscillator is used as the chip clock, the watchdog oscillator will still be used for the watchdog timer and for the reset time-out. for more information on the pre-programmed cali- bration value, see the section ?calibration bytes? on page 105 . table 6-3. start-up times for the external clock selection sut1..0 start-up time from power-down and power-save additional delay from reset recommended usage 00 6 ck 14ck bod enabled 01 6 ck 14ck + 4 ms fast rising power 10 6 ck 14ck + 64 ms slowly rising power 11 reserved table 6-4. internal calibrated rc o scillator operating modes cksel1..0 nominal frequency 10 (1) 9.6 mhz 01 4.8 mhz
26 8126d?avr?11/09 attiny13a when this oscillator is selected, start-up ti mes are determined by the sut fuses as shown in table 6-5 . notes: 1. the device is shipped with this option selected. 2. if the rstdisbl fuse is progra mmed, this start-up time will be increased to 14ck + 4 ms to ensure programming mode can be entered. 6.2.3 internal 128 khz oscillator the 128 khz internal oscillator is a low power oscillator providing a clock of 128 khz. the fre- quency depends on supply voltage, temperature and batch variations. this clock may be select as the system clock by progra mming the cksel fuses to ?11?. when this clock source is selected, start-up times are determined by the sut fuses as shown in table 6-6 . note: 1. if the rstdisbl fuse is pr ogrammed, this start-up time will be increased to 14ck + 4 ms to ensure programming mode can be entered. 6.2.4 default clock source the device is shipped with cksel = ?10?, sut = ?10?, and ckdiv8 programmed. the default clock source setting is therefor e the internal rc oscillator runn ing at 9.6 mhz with longest start- up time and an initial system clock prescaling of 8. this default setting ensures that all users can make their desired clock source setting us ing an in-system or high-voltage programmer. 6.3 system clock prescaler the attiny13a system clock can be divided by setting the ?clkpr ? clock prescale register? on page 28 . this feature can be used to decrease power consumption when the requirement for processing power is low. this can be used with al l clock source options, and it will affect the clock frequency of the cpu and all synchronous peripherals. clk i/o , clk adc , clk cpu , and clk flash are divided by a factor as shown in table 6-8 on page 28 . table 6-5. start-up times for the internal calib rated rc oscillato r clock selection sut1..0 start-up time from power-down additional delay from reset (v cc = 5.0v) recommended usage 00 6 ck 14ck (2) bod enabled 01 6 ck 14ck + 4 ms fast rising power 10 (1) 6 ck 14ck + 64 ms slowly rising power 11 reserved table 6-6. start-up times for the 128 khz internal oscillator sut1:0 start-up time from power-down and power-save additional delay from reset recommended usage 00 6 ck 14ck (1) bod enabled 01 6 ck 14ck + 4 ms fast rising power 10 6 ck 14ck + 64 ms slowly rising power 11 reserved
27 8126d?avr?11/09 attiny13a 6.3.1 switching time when switching between prescaler settings, the system clock prescaler ensures that no glitches occur in the clock system and that no intermediate frequency is higher than neither the clock frequency corresponding to the previous se tting, nor the clock frequency corresponding to the new setting. the ripple counter that implements the prescale r runs at the frequency of the undivided clock, which may be faster than the cpu?s clock frequency. hence, it is not possible to determine the state of the prescaler ? even if it were readable, and the exact time it takes to switch from one clock division to another cannot be exactly predicted. from the time the clkps values ar e written, it takes between t1 + t2 and t1 + 2*t2 before the new clock frequency is active. in this interval, 2 active clock edges are produced. here, t1 is the previous clock period, and t2 is the period corresponding to the new prescaler setting. 6.4 register description 6.4.1 osccal ? oscillato r calibration register ? bit 7 ? res: reserved bit this bit is reserved bit in atti ny13a and it will always read zero. ? bits 6:0 ? cal[6:0]: oscillator calibration value writing the calibration byte to this address will trim the internal oscillator to remove process vari- ations from the oscillator frequency. this is done automatically during chip reset. when osccal is zero, the lowest available frequency is chosen. writing non-zero values to this regis- ter will increase the frequency of the internal oscillator. writing 0x7f to the register gives the highest available frequency. the calibrated oscillator is used to time eeprom and flash access. if eeprom or flash is written, do not calibrate to more than 10% above the nominal frequency. otherwise, the eeprom or flash write may fail. no te that the oscillator is inten ded for calibration to 9.6 mhz or 4.8 mhz. tuning to other values is not guaranteed, as indicated in table 6-7 below. to ensure stable operation of the mcu the calibration value should be changed in small steps. a variation in frequency of more than 2% from one cycle to the next can lead to unpredicatble behavior. changes in osccal should not exceed 0x20 for each calibration. it is required to ensure that the mcu is kept in reset during such changes in the clock frequency bit 76543210 0x31 ? cal6 cal5 cal4 cal3 cal2 cal1 cal0 osccal read/write r r/w r/w r/w r/w r/w r/w r/w initial value 0 device specific calibration value table 6-7. internal rc oscillator frequency range osccal value typical lowest frequency with respect to nominal frequency typical highest frequency with respect to nominal frequency 0x00 50% 100% 0x3f 75% 150% 0x7f 100% 200%
28 8126d?avr?11/09 attiny13a 6.4.2 clkpr ? clock prescale register ? bit 7 ? clkpce: clock prescaler change enable the clkpce bit must be written to logic one to enable ch ange of the clkps bits. the clkpce bit is only updated when the other bits in cl kpr are simultaneously wr itten to zero. clkpce is cleared by hardware four cycles af ter it is written or when the clkps bits are written. rewriting the clkpce bit within this time-out period does ne ither extend the time-out period, nor clear the clkpce bit. ? bits 6:4 ? res: reserved bits these bits are reserved bits in the attiny13a and will always read as zero. ? bits 3:0 ? clkps3:0: clock prescaler select bits 3 - 0 these bits define the division factor between th e selected clock source and the internal system clock. these bits can be written run-time to va ry the clock frequency to suit the application requirements. as the divider divides the master cl ock input to the mcu, the speed of all synchro- nous peripherals is reduced when a division fact or is used. the division factors are given in table 6-8 on page 28 . to avoid unintentional changes of clock frequency, a special write procedure must be followed to change the clkps bits: 1. write the clock prescaler change enable (clkpce) bit to one and all other bits in clkpr to zero. 2. within four cycles, write the desired valu e to clkps while writin g a zero to clkpce. interrupts must be disabled when changing prescaler setting to make sure the write procedure is not interrupted.hee setting. the application softwa re must ensure that a sufficient division factor is chosen if the selected clock source has a higher frequency than the maximum frequency of the device at the present op erating conditions. the device is shipped with the ckdiv8 fuse programmed. bit 7 6 5 4 3 2 1 0 0x26 clkpce ? ? ? clkps3 clkps2 clkps1 clkps0 clkpr read/write r/w r r r r/w r/w r/w r/w initial value 0 0 0 0 see bit description table 6-8. clock prescaler select clkps3 clkps2 clkps1 clkps0 clock division factor 0000 1 0001 2 0010 4 0011 8 0100 16 0101 32 0110 64 0111 128 1000 256
29 8126d?avr?11/09 attiny13a 1001 reserved 1010 reserved 1011 reserved 1100 reserved 1101 reserved 1110 reserved 1111 reserved table 6-8. clock prescaler select (continued) clkps3 clkps2 clkps1 clkps0 clock division factor
30 8126d?avr?11/09 attiny13a 7. power management and sleep modes the high performance and industry leading code ef ficiency makes the avr microcontrollers an ideal choise for low power applications. in addition, sleep modes enable the application to shut down unused modules in the mcu, thereby saving power. the avr provides various sleep modes allowing the user to tailor the power consumption to the application?s requirements. 7.1 sleep modes figure 6-1 on page 23 presents the different clock systems in the attiny13a, and their distribu- tion. the figure is helpful in selecting an appropriate sleep mode. table 7-1 shows the different sleep modes and their wake up sources. note: 1. for int0, only level interrupt. to enter any of the three sleep modes, the se bit in mcucr must be written to logic one and a sleep instruction must be executed. the sm1..0 bits in the mcucr register select which sleep mode (idle, adc noise reduction, or po wer-down) will be activated by the sleep instruc- tion. see table 7-2 on page 34 for a summary. if an enabled interrupt occurs while the mcu is in a sleep mode, the mcu wakes up. the mcu is then halted for four cycles in addition to the st art-up time, executes the interrupt routine, and resumes execution from the instruction followi ng sleep. the contents of the register file and sram are unaltered when the device wakes up from sleep. if a reset occurs during sleep mode, the mcu wakes up and executes from the reset vector. note that if a level triggered interrupt is used for wake-up from power-down mode, the changed level must be held for some time to wake up the mcu. refer to ?external interrupts? on page 46 for details. 7.1.1 idle mode when the sm[1:0] bits are writt en to 00, the sleep instruction makes the mcu enter idle mode, stopping the cpu but allowing analog comparator , adc, timer/counter, watchdog, and the interrupt system to continue operatin g. this sleep mode basically halts clk cpu and clk flash , while allowing the other clocks to run. idle mode enables the mcu to wake up from external triggered interrupts as well as internal ones like the timer overflow. if wake-up from the analog comparator interrupt is not required, the analog comparator can be powered down by setting the acd bit in the analog comparator table 7-1. active clock domains and wake-up sources in the different sleep modes active clock domains oscillators wake-up sources sleep mode clk cpu clk flash clk io clk adc main clock source enabled int0 and pin change spm/ eeprom ready adc other i/o watchdog interrupt idle x x x x x x x x adc noise reduction xxx (1) xx x power-down x (1) x
31 8126d?avr?11/09 attiny13a control and status register ? acsr. this will reduce power consumption in idle mode. if the adc is enabled, a conversion starts automatically when this mode is entered. 7.1.2 adc noise reduction mode when the sm[1:0] bits are written to 01, the sleep instruction makes the mcu enter adc noise reduction mode, stopping the cpu but allo wing the adc, the external interrupts, and the watchdog to continue operating (if enabled). this sleep mode halts clk i/o , clk cpu , and clk flash , while allowing the ot her clocks to run. this improves the noise envir onment for the adc, enabling hig her resolution measurements. if the adc is enabled, a conversion starts automati cally when this mode is entered. apart form the adc conversion complete interrupt, only an external reset, a watchdog reset, a brown-out reset, an spm/eeprom ready inte rrupt, an external level interr upt on int0 or a pin change interrupt can wake up the mcu from adc noise reduction mode. 7.1.3 power-down mode when the sm[1:0] bits are written to 10, the sleep instruction makes the mcu enter power- down mode. in this mode, the os cillator is stopped, while the ex ternal interrupts, and the watch- dog continue operating (if enabled). only an external reset, a watchdog reset, a brown-out reset, an external level interrupt on int0, or a pin change interrupt can wake up the mcu. this sleep mode halts all generated clocks, allowi ng operation of asynchronous modules only. 7.2 software bod disable when the brown-out detector (bod) is enabled by bodlevel fuses (see table 17-3 on page 104 ), the bod is actively monitoring the supply voltage during a sleep period. it is possible to save power by disabling the bod by software in power-down sleep mode. the sleep mode power consumption will then be at the same leve l as when bod is glo bally disabled by fuses. if bod is disabled by software, the bod function is turned off immediately after entering the sleep mode. upon wake-up from sleep, bod is automatically enabled again. this ensures safe operation in case the v cc level has dropped during the sleep period. when the bod has been disabled, the wake-up time from sleep mode will be approximately 60s to ensure that the bod is working corr ectly before the mcu continues executing code. bod disable is controlled by the bods (bod sleep) bit of bod control register, see ?bodcr ? brown-out detector control register? on page 33 . writing this bit to one turns off bod in power-down and stand-by, while writing a zero keeps the bod active. the default setting is zero, i.e. bod active. writing to the bods bit is controlled by a tim ed sequence and an enable bit, see ?bodcr ? brown-out detector control register? on page 33 . 7.3 power reduction register the power reduction register (see ?prr ? power reduction register? on page 34 ) provides a method to reduce power consumption by stopping the clock to individual peripherals. the cur- rent state of the peripheral is frozen and the i/o registers can not be read or written. when stopping the clock resources used by the peripheral will remain occupied, hence the peripheral should in most cases be disabl ed before stopping the clock. waki ng up a module (by clearing the bit in prr) puts the module in the same state as before shutdown.
32 8126d?avr?11/09 attiny13a modules can be shut down in idle and active mode s, significantly helpi ng to reduce the overall power consumption. in all other sleep mo des, the clock is already stopped. see ?supply current of i/o modules? on page 124 for examples. 7.4 minimizing power consumption there are several issues to consider when tryi ng to minimize the power consumption in an avr controlled system. in general, sleep modes shoul d be used as much as possible, and the sleep mode should be selected so that as few as possi ble of the device?s func tions are operating. all functions not needed should be disabled. in parti cular, the following modules may need special consideration when trying to achieve th e lowest possible power consumption. 7.4.1 analog to digital converter if enabled, the adc will be enabled in all sleep modes. to save power, the adc should be dis- abled before entering any sleep mode. when th e adc is turned off and on again, the next conversion will be an extende d conversion. refer to ?analog to digital converter? on page 82 for details on adc operation. 7.4.2 analog comparator when entering idle mode, the analog comparator should be disabled if not used. when entering adc noise reduction mode, the analog comparat or should be disabled. in the other sleep modes, the analog comparator is automatically disabled. however, if the analog comparator is set up to use the internal voltage reference as input, the analog comp arator should be dis- abled in all sleep modes. ot herwise, the internal volt age reference will be enabled, independent of sleep mode. refer to ?analog comparator? on page 79 for details on how to con- figure the analog comparator. 7.4.3 brown-out detector if the brown-out detector is not needed in the ap plication, this module should be turned off. if the brown-out detector is enabled by the bodlevel fuses, it will be enabled in all sleep modes, and hence, always consume power. in the deeper sleep modes, this will contribute significantly to the total current consumption. see ?brown-out detection? on page 37 and ?software bod dis- able? on page 31 for details on how to configure the brown-out detector. 7.4.4 internal voltage reference the internal voltage referenc e will be enabled when needed by the brown-out de tection, the analog comparator or the adc. if these modul es are disabled as described in the sections above, the internal voltage refe rence will be disabled and it w ill not be consuming power. when turned on again, the user must allow the referenc e to start up before the output is used. if the reference is kept on in sleep mode, the output can be used immediately. refer to ?internal volt- age reference? on page 38 for details on the start-up time. 7.4.5 watchdog timer if the watchdog timer is not needed in the application, this module should be turned off. if the watchdog timer is enabled, it will be enabled in all sleep modes, and hence, always consume power. in the deeper slee p modes, this will contribute signific antly to the total current consump- tion. refer to ?interrupts? on page 45 for details on how to configure the watchdog timer.
33 8126d?avr?11/09 attiny13a 7.4.6 port pins when entering a sleep mode, all port pins should be configured to use minimum power. the most important thing is then to ensure that no pins drive resistive loads. in sleep modes where both the i/o clock (clk i/o ) and the adc clock (clk adc ) are stopped, the input buffers of the device will be disabled. this ensu res that no power is consumed by the input logic when not needed. in some cases, the input logic is needed for detec ting wake-up conditions, and it will then be enabled. refer to the section ?digital input enable and sleep modes? on page 53 for details on which pins are enabled. if the input buffer is enab led and the input signal is left floating or has an analog signal level close to v cc /2, the input buffer will use excessive power. for analog input pins, the digital input buffer should be disabled at all times. an analog signal level close to v cc /2 on an input pin can cause significant current even in active mode. digital input buffers can be disabled by writing to the digital input disable regi ster (didr0). refer to ?didr0 ? digital input disable register 0? on page 81 for details. 7.5 register description 7.5.1 bodcr ? brown-out detector control register the bod control register cont ains control bits for disabling the bod by software. ? bit 1 ? bods: bod sleep in order to disable bod during sleep the bods bit must be written to logic one. this is controlled by a timed sequence and the enable bit, bodse. first, both bods and bodse must be set to one. second, within four clock cycles, bods must be set to one and bodse must be set to zero. the bods bit is active thre e clock cycles after it is set. a sleep instruction must be exe- cuted while bods is acti ve in order to turn o ff the bod for the actual sleep mode. the bods bit is automatically cleared after three clock cycles. ? bit 0 ? bodse: bod sleep enable the bodse bit enables setting of bods control bit, as explained on bods bit description. bod disable is controlled by a timed sequence. 7.5.2 mcucr ? mcu control register the mcu control register contains control bits for power management. ? bit 5 ? se: sleep enable the se bit must be written to logic one to make the mcu enter the sleep mode when the sleep instruction is executed. to avoid the mcu entering the sleep mode unless it is the programmer?s purpose, it is recommended to wr ite the sleep enable (se) bit to one just before the execution of the sleep instruction and to clear it immediately af ter waking up. bit 76543210 0x30 ? ? ? ? ? ? bods bodse bodcr read/writerrrrrrr/wr/w initial value 0 0 0 0 0 0 0 0 bit 76543210 0x35 ? pud se sm1 sm0 ? isc01 isc00 mcucr read/write r r/w r/w r/w r/w r r/w r/w initial value 0 0 0 0 0 0 0 0
34 8126d?avr?11/09 attiny13a ? bits 4:3 ? sm[1:0]: sleep mode select bits 1:0 these bits select between the three available sleep modes as shown in table 7-2 on page 34 . 7.5.3 prr ? power reduction register the power reduction register provides a met hod to reduce power consumption by allowing peripheral clock signals to be disabled. ? bits 7:2 ? res: reserved bits these bits are reserved and will always read as zero. ? bit 1 ? prtim0: power reduction timer/counter0 writing a logic one to this bit shuts down the timer/counter0 module. when the timer/counter0 is enabled, operation will cont inue like before the shutdown. ? bit 0 ? pradc: power reduction adc writing a logic one to this bit shuts down the a dc. the adc must be disabled before shut down. the analog comparator cannot be used when the adc is shut down. table 7-2. sleep mode select sm1 sm0 sleep mode 00idle 0 1 adc noise reduction 1 0 power-down 11reserved bit 7654 3 2 10 0x25 ? ? ? ? ? ? prtim0 pradc prr read/write r r r r r r r/w r/w initial value 0 0 0 0 0 0 0 0
35 8126d?avr?11/09 attiny13a 8. system control and reset 8.1 resetting the avr during reset, all i/o registers are set to their initial values, and the pr ogram starts execution from the reset vector. the instruction placed at the reset vector must be a rjmp ? relative jump ? instruction to the reset handling routine. if the program never enables an interrupt source, the interrupt vectors are not used, and regular program code can be placed at these locations. the circuit diagram in figure 8-1 on page 35 shows the reset logic. ?system and reset characteristics? on page 120 defines the electrical parame ters of the reset circuitry. figure 8-1. reset logic the i/o ports of the avr are immediately reset to their initial state when a reset source goes active. this does not require any clock source to be running. after all reset sources have gone inactive, a delay counter is invoked, stretching the internal reset. this allows the power to reach a stable level before normal operation starts. the time-out period of the delay coun ter is defined by the us er through the sut and c ksel fuses. the differ- ent selections for the delay period are presented in ?clock sources? on page 24 . mcu s t a t us regi s ter (mcu s r) brown-o u t re s et circ u it bodlevel [1..0] del a y co u nter s ck s el[1:0] ck timeout wdrf borf extrf porf data b u s clock gener a tor s pike filter p u ll- u p re s i s tor w a tchdog o s cill a tor s ut [ 1:0 ] power-on re s et circ u it
36 8126d?avr?11/09 attiny13a 8.2 reset sources the attiny13a has four sources of reset: ? power-on reset. the mcu is reset when the supply voltage is below the power-on reset threshold (v pot ). ? external reset. the mcu is reset when a low level is pr esent on the reset pin for longer than the minimum pulse length. ? watchdog reset. the mcu is reset when the watchdog timer period expires and the watchdog is enabled. ? brown-out reset. the mcu is re set when the supply voltage v cc is below the brown-out reset threshold (v bot ) and the brown-out detector is enabled. 8.2.1 power-on reset a power-on reset (por) pulse is generated by an on-chip detection circuit. the detection level is defined in ?system and reset characteristics? on page 120 . the por is activated whenever v cc is below the detection level. the por circuit can be used to trigger the start-up reset, as well as to detect a fa ilure in supply voltage. a power-on reset (por) circuit ensures that the device is reset from power-on. reaching the power-on reset threshold voltage invokes the delay counter, which determines how long the device is kept in reset after v cc rise. the reset signal is acti vated again, without any delay, when v cc decreases below the detection level. figure 8-2. mcu start-up, reset tied to v cc figure 8-3. mcu start-up, reset extended externally v reset time-out internal reset t tout v pot v rst cc reset time-out internal reset t tout v pot v rst v cc
37 8126d?avr?11/09 attiny13a 8.2.2 external reset an external reset is generated by a low level on the reset pin if enabled. reset pulses longer than the minimum pulse width ( see ?system and reset characteristics? on page 120. ) will gen- erate a reset, even if the clock is not running. shorter pulses are not guaranteed to generate a reset. when the applied signal reaches the reset threshold voltage ? v rst ? on its positive edge, the delay counter starts the mcu after the time-out period ? t tout ? has expired. figure 8-4. external reset during operation 8.2.3 brown-out detection attiny13a has an on-chip brown-out detection (bod) circuit for monitoring the v cc level during operation by comparing it to a fixed trigger level. the trigger level for the bod can be selected by the bodlevel fuses. the trigger level has a hysteresis to ensure spike free brown-out detection. the hysteresis on the dete ction level should be interpreted as v bot+ = v bot + v hyst /2 and v bot- = v bot - v hyst /2. when the bod is enabled, and v cc decreases to a value below the trigger level (v bot- in figure 8-5 on page 37 ), the brown-out reset is i mmediately activated. when v cc increases above the trigger level (v bot+ in figure 8-5 on page 37 ), the delay counter starts the mcu after the time- out period t tout has expired. the bod circuit will only detect a drop in v cc if the voltage stays below the trigger level for lon- ger than t bod given in ?system and reset characteristics? on page 120 . figure 8-5. brown-out reset during operation cc v cc reset time-out internal reset v bot- v bot+ t tout
38 8126d?avr?11/09 attiny13a 8.2.4 watchdog reset when the watchdog times out, it will generate a short reset pulse of one ck cycle duration. on the falling edge of this pulse, the delay ti mer starts counting the time-out period t tout . refer to ?interrupts? on page 45 for details on operation of the watchdog timer. figure 8-6. watchdog reset du ring operation 8.3 internal voltage reference attiny13a features an internal bandgap reference. this reference is used for brown-out detec- tion, and it can be used as an input to the analog comparator or the adc. 8.3.1 voltage reference enable signals and start-up time the voltage reference has a start-up time that may influence the way it should be used. the start-up time is given in ?system and reset characteristics? on page 120 . to save power, the reference is not always turned on. the reference is on during the following situations: ? when the bod is enabled (by programming the bodlevel [1.. 0] fuse). ? when the bandgap reference is connected to th e analog comparator (by setting the acbg bit in acsr). ? when the adc is enabled. thus, when the bod is not enabled, after setting the acbg bit or enabling the adc, the user must always allow the reference to start up before the output from the analog comparator or adc is used. to reduce power consumption in power-down mode, the user can avoid the three conditions above to ensure that the reference is turned off before entering power-down mode. 8.4 watchdog timer attiny13a has an enhanced watchdog timer (wdt). the wdt is a timer counting cycles of a separate on-chip 128 khz oscillator. the wdt gi ves an interrupt or a system reset when the counter reaches a given time-out value. in norma l operation mode, it is required that the system uses the wdr - watchdog timer reset - instructi on to restart the counter before the time-out ck cc
39 8126d?avr?11/09 attiny13a value is reached. if the system doesn't restart the counter, an interrupt or system reset will be issued. figure 8-7. watchdog timer in interrupt mode, the wdt gives an interrupt when the timer expires. this interrupt can be used to wake the device from sleep-modes, and also as a general system timer. one example is to limit the maximum time allowed for certain opera tions, giving an interrupt when the operation has run longer than expected. in system reset mode, the wdt gives a reset when the timer expires. this is typically used to prevent sys tem hang-up in case of runaway code. the third mode, interrupt and system reset mode, combines the other two modes by first giving an inter- rupt and then switch to system reset mode. th is mode will for instance allow a safe shutdown by saving critical parameters before a system reset. the watchdog always on (wdton ) fuse, if programmed, will forc e the watchdog timer to sys- tem reset mode. with the fuse programmed the system reset mode bit (wde) and interrupt mode bit (wdtie) are locked to 1 and 0 respective ly. to further ensure program security, altera- tions to the watchdog set-up must follow timed sequences. th e sequence for clearing wde and changing time-out configuration is as follows: 1. in the same operation, write a logic one to the watchdog change enable bit (wdce) and wde. a logic one must be written to wde regardless of the previous value of the wde bit. 2. within the next four clock cycles, write the wde and watchdog prescaler bits (wdp) as desired, but with the wdce bit cleared. this must be done in one operation. 128khz oscillator osc/2k osc/4k osc/8k osc/16k osc/32k osc/64k osc/128k osc/256k osc/512k osc/1024k wdp0 wdp1 wdp2 wdp3 watchdog reset wde wdtif wdtie mcu reset interrupt
40 8126d?avr?11/09 attiny13a the following code example shows one assembly and one c function for turning off the watch- dog timer. the example assumes that interrupts are controlled (e.g. by disabling interrupts globally) so that no interrupts will occur during th e execution of these functions. note: see ?code examples? on page 6 . if the watchdog is accidentally enabled, for example by a runaway pointer or brown-out condi- tion, the device will be reset and the watchdog timer will stay enabl ed. if the code is not set up to handle the watchdog, this might lead to an eter nal loop of time-out resets. to avoid this situa- assembly code example wdt_off: ; turn off global interrupt cli ; reset watchdog timer wdr ; clear wdrf in mcusr in r16, mcusr andi r16, (0xff - (1< 41 8126d?avr?11/09 attiny13a tion, the application software should always cl ear the watchdog system reset flag (wdrf) and the wde control bit in the initialisation routine, even if the watchdog is not in use. the following code example shows one assembly and one c function for changing the time-out value of the watchdog timer. note: see ?code examples? on page 6 . the watchdog timer should be reset before any change of the wdp bits, since a change in the wdp bits can result in a time-out when switching to a shorter time-out period. assembly code example wdt_prescaler_change: ; turn off global interrupt cli ; reset watchdog timer wdr ; start timed sequence in r16, wdtcr ori r16, (1< 42 8126d?avr?11/09 attiny13a 8.5 register description 8.5.1 mcusr ? mcu status register the mcu status register provides information on which reset source caused an mcu reset. ? bits 7:4 ? res: reserved bits these bits are reserved bits in the attiny13a and will always read as zero. ? bit 3 ? wdrf: watchdog reset flag this bit is set if a watchdog re set occurs. the bit is reset by a power-on reset, or by writing a logic zero to the flag. ? bit 2 ? borf: brown-out reset flag this bit is set if a brown-out reset occurs. the bi t is reset by a power-on reset, or by writing a logic zero to the flag. ? bit 1 ? extrf: external reset flag this bit is set if an external reset occurs. the bit is reset by a power-on reset, or by writing a logic zero to the flag. ? bit 0 ? porf: power-on reset flag this bit is set if a power-on reset occurs. the bit is reset only by writing a logic zero to the flag. to make use of the reset flags to identify a rese t condition, the user should read and then reset the mcusr as early as possible in the program. if the register is cleared before another reset occurs, the source of the reset can be found by examining the reset flags. 8.5.2 wdtcr ? watchdog timer control register ? bit 7 ? wdtif: watchdog timer interrupt flag this bit is set when a time-out occurs in the watchdog timer and the watchdog timer is config- ured for interrupt. wdtif is cleared by hardwa re when executing the corresponding interrupt handling vector. alternatively, wdtif is cleared by writing a logic one to the flag. when the i-bit in sreg and wdtie are set, the watchdog time-out interrupt is executed. ? bit 6 ? wdtie: watchdog timer interrupt enable when this bit is written to one and the i-bit in t he status register is set, the watchdog interrupt is enabled. if wde is cleared in combination with this setting, the watchdog timer is in interrupt mode, and the corresponding interrupt is execut ed if time-out in the watchdog timer occurs. if wde is set, the watchdog timer is in interrupt and system reset mode. the first time-out in the watchdog timer will set wdtif. executing th e corresponding interrupt vector will clear wdtie and wdtif automatically by hardware (the watchdog goes to system reset mode). bit 76543210 0x34 ????wdrfborfextrfporfmcusr read/write rrrrr/wr/wr/wr/w initial value0000 see bit d escription bit 76543210 0x21 wdtif wdtie wdp3 wdce wde wdp2 wdp1 wdp0 wdtcr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 x 0 0 0
43 8126d?avr?11/09 attiny13a this is useful for keeping the watchdog timer secu rity while using the interrupt. to stay in inter- rupt and system reset mode, wdtie must be set after each interrupt. this should however not be done within the interrupt service routine itsel f, as this might compromi se the safety-function of the watchdog system reset mode. if the interrupt is not executed before the next time-out, a system reset will be applied. note: 1. wdton fuse set to ?0? means programmed and ?1? means unprogrammed. ? bit 4 ? wdce: watchdog change enable this bit is used in timed sequences for changi ng wde and prescaler bits. to clear the wde bit, and/or change the prescaler bits, wdce must be set. once written to one, ha rdware will clear wdce after four clock cycles. ? bit 3 ? wde: watchdog system reset enable wde is overridden by wdrf in mcusr. this m eans that wde is always set when wdrf is set. to clear wde, wdrf must be cleared first. this feature ensures multiple resets during con- ditions causing failure, and a safe start-up after the failure. ? bit 5, 2:0 ? wdp[3:0]: watchdog timer prescaler 3, 2, 1 and 0 the wdp[3:0] bits determine the watchdog time r prescaling when the watchdog timer is run- ning. the different prescaling values and their corresponding ti me-out periods are shown in table 8-2 on page 43 .. table 8-1. watchdog timer configuration wdton (1) wde wdtie mode action on time-out 1 0 0 stopped none 1 0 1 interrupt mode interrupt 1 1 0 system reset mode reset 111 interrupt and system reset mode interrupt, then go to system reset mode 0 x x system reset mode reset table 8-2. watchdog timer prescale select wdp3 wdp2 wdp1 wdp0 number of wdt oscillator cycles typical time-out at v cc = 5.0v 0000 2k (2048) cycles 16 ms 0001 4k (4096) cycles 32 ms 0010 8k (8192) cycles 64 ms 0011 16k (16384) cycles 0.125 s 0100 32k (32768) cycles 0.25 s 0101 64k (65536) cycles 0.5 s 0110 128k (131072) cycles 1.0 s 0111 256k (262144) cycles 2.0 s 1000 512k (524288) cycles 4.0 s 1001 1024k (1048576) cycles 8.0 s
44 8126d?avr?11/09 attiny13a 1010 reserved 1011 1100 1101 1110 1111 table 8-2. watchdog timer prescale select (continued) wdp3 wdp2 wdp1 wdp0 number of wdt oscillator cycles typical time-out at v cc = 5.0v
45 8126d?avr?11/09 attiny13a 9. interrupts this section describes the specifics of the interrupt handling as performed in attiny13a. for a general explanation of the avr in terrupt handling, refer to ?reset and interrupt handling? on page 12 . 9.1 interrupt vectors the interrupt vectors of attiny13a are described in table 9-1 below. if the program never enables an interrupt source, the interrupt vectors are not used, and regular program code can be placed at these locations. the most typical and general program setup fo r the reset and interrupt vector addresses in attiny13a is: address labels code comments 0x0000 rjmp reset ; reset handler 0x0001 rjmp ext_int0 ; irq0 handler 0x0002 rjmp pcint0 ; pcint0 handler 0x0003 rjmp tim0_ovf ; timer0 overflow handler 0x0004 rjmp ee_rdy ; eeprom ready handler 0x0005 rjmp ana_comp ; analog comparator handler 0x0006 rjmp tim0_compa ; timer0 comparea handler 0x0007 rjmp tim0_compb ; timer0 compareb handler 0x0008 rjmp watchdog ; watchdog interrupt handler 0x0009 rjmp adc ; adc conversion handler ; 0x000a reset: ldi r16, low(ramend); main program start 0x000b out spl,r16 ; set stack pointer to top of ram 0x000c sei ; enable interrupts 0x000d xxx ... ... ... ... table 9-1. reset and interrupt vectors vector no. program address source interrupt definition 1 0x0000 reset external pin, power-on reset, brown-out reset, watchdog reset 2 0x0001 int0 external interrupt request 0 3 0x0002 pcint0 pin change interrupt request 0 4 0x0003 tim0_ovf timer/counter overflow 5 0x0004 ee_rdy eeprom ready 6 0x0005 ana_comp analog comparator 7 0x0006 tim0_compa timer/counter compare match a 8 0x0007 tim0_compb timer/counter compare match b 9 0x0008 wdt watchdog time-out 10 0x0009 adc adc conversion complete
46 8126d?avr?11/09 attiny13a 9.2 external interrupts the external interrupts are triggered by the int0 pin or any of the pcin t5..0 pins. observe that, if enabled, the interrupts will trigger even if t he int0 or pcint5..0 pins are configured as out- puts. this feature provides a way of generating a software interrupt. pin change interrupts pci will trigger if any enabled pcint5..0 pin toggl es. the pcmsk register control which pins con- tribute to the pin change interrupts. pin change interrupts on pcint5..0 are detected asynchronously. this implies that these interrupts can be used for waking the part also from sleep modes other than idle mode. the int0 interrupts can be triggered by a falling or rising edge or a low level. this is set up as indicated in the specification for the mcu control register ? mcucr. when the int0 interrupt is enabled and is configured as leve l triggered, the interr upt will trigger as long as the pin is held low. note that recognition of falling or rising edge interrupts on int0 requires the presence of an i/o clock, described in ?clock systems and their distribution? on page 23 . 9.2.1 low level interrupt a low level interrupt on int0 is detected asynch ronously. this implies that this interrupt can be used for waking the part also from sleep modes other than idle mode. the i/o clock is halted in all sleep modes except idle mode. note that if a level triggered interrupt is used for wake-up from power-down, the required level must be held long enough for the mcu to complete t he wake-up to trigger the level interrupt. if the level disappears before the end of the start-up ti me, the mcu will still wake up, but no inter- rupt will be generate d. the start-up time is defined by the sut and c ksel fuses as described in ?system clock and clock options? on page 23 . if the low level on the interrupt pin is removed before the device has woken up then program execution will not be diverted to the interrupt service ro utine but continue from the instruction fol- lowing the sleep command. 9.2.2 pin change interrupt timing an example of timing of a pin change interrupt is shown in figure 9-1 below. figure 9-1. timing of pin change interrupts clk pcint(n) pin_lat pin_sync pcint_in_(n) pcint_syn p cint_setflag pcif pcint(0) pin_sync pcint_syn pin_lat d q le pcint_setflag pc if clk clk pcint(0) in pcmsk(x) pcint_in_(0) 0 x
47 8126d?avr?11/09 attiny13a 9.3 register description 9.3.1 mcucr ? mcu control register the external interrupt control register a cont ains control bits for interrupt sense control. ? bits 1, 0 ? isc01, isc00: interrupt sense control 0 bit 1 and bit 0 the external interrupt 0 is activated by the exte rnal pin int0 if the sr eg i-flag and the corre- sponding interrupt mask are set. the level and edges on the external int0 pin that activate the interrupt are defined in table 9-2 on page 47 . the value on the int0 pin is sampled before detecting edges. if edge or toggle interrupt is se lected, pulses that last longer than one clock period will generate an interrupt. shorter pulses are not guaranteed to generate an interrupt. if low level interrupt is selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt. 9.3.2 gimsk ? general interrupt mask register ? bits 7, 4:0 ? res: reserved bits these bits are reserved bits in the attiny13a and will always read as zero. ? bit 6 ? int0: external interrupt request 0 enable when the int0 bit is set (one) and the i-bit in th e status register (sreg) is set (one), the exter- nal pin interrupt is enabled. the interrupt sense control0 bits 1/0 (isc01 and isc00) in the mcu control register (mcu cr) define whether the external interrup t is activated on rising and/or fall- ing edge of the int0 pin or level sensed. activi ty on the pin will cause an interrupt request even if int0 is configured as an output. the corresponding interrupt of external interrupt request 0 is executed from the in t0 interrupt vector. ? bit 5 ? pcie: pin change interrupt enable when the pcie bit is set (one) and the i-bit in the status register (sreg) is set (one), pin change interrupt is enabled. any change on any ena bled pcint5..0 pin will cause an interrupt. the corresponding interrupt of pin change interr upt request is executed from the pci interrupt vector. pcint5..0 pins are enabled individually by the pcmsk register. bit 76543210 0x35 ? pud se sm1 sm0 ? isc01 isc00 mcucr read/write r r/w r/w r/w r/w r r/w r/w initial value00000000 table 9-2. interrupt 0 sense control isc01 isc00 description 0 0 the low level of int0 generates an interrupt request. 0 1 any logical change on int0 generates an interrupt request. 1 0 the falling edge of int0 generates an interrupt request. 1 1 the rising edge of int0 generates an interrupt request. bit 76543210 0x3b ?int0pcie?????gimsk read/writerr/wr/wrrrrr initial value00000000
48 8126d?avr?11/09 attiny13a 9.3.3 gifr ? general in terrupt flag register ? bits 7, 4:0 ? res: reserved bits these bits are reserved bits in the attiny13a and will always read as zero. ? bit 6 ? intf0: external interrupt flag 0 when an edge or logic change on the int0 pin triggers an interrupt request, intf0 becomes set (one). if the i-bit in sreg and the int0 bit in gimsk are set (o ne), the mcu will jump to the cor- responding interrupt vector. the flag is cleared when the interrupt routine is executed. alternatively, the flag can be clea red by writing a logical one to it. this flag is always cleared when int0 is configured as a level interrupt. ? bit 5 ? pcif: pin change interrupt flag when a logic change on any pcint5 : 0 pin triggers an interrupt request, pcif becomes set (one). if the i-bit in sreg and the pcie bit in gimsk are set (one), the mcu will jump to the cor- responding interrupt vector. the flag is cleared when the interrupt routine is executed. alternatively, the flag can be cleared by writing a logical one to it. 9.3.4 pcmsk ? pin change mask register ? bits 7, 6 ? res: reserved bits these bits are reserved bits in the attiny13a and will always read as zero. ? bits 5:0 ? pcint5:0: pin change enable mask 5:0 each pcint5 : 0 bit selects whether pin change interrupt is enabled on the corresponding i/o pin. if pcint5 : 0 is set and the pcie bit in gimsk is set, pin change interrupt is enabled on the cor- responding i/o pin. if pcint5 : 0 is cleared, pin change interrup t on the corresponding i/o pin is disabled. bit 76543210 0x3a ?intf0pcif?????gifr read/writerr/wr/wrrrrr initial value00000000 bit 76543210 0x15 ? ? pcint5 pcint4 pcint3 p cint2 pcint1 pcint0 pcmsk read/write r r r/w r/w r/w r/w r/w r/w initial value00000000
49 8126d?avr?11/09 attiny13a 10. i/o ports 10.1 overview all avr ports have true read-modi fy-write functionality when used as general digital i/o ports. this means that the dire ction of one port pin can be chan ged without unintentionally changing the direction of any other pin with the sbi and cbi instructions. the same applies when chang- ing drive value (if configured as output) or enab ling/disabling of pull-up resistors (if configured as input). each output buffer has symmetrical drive characteristics with both high sink and source capability. the pin driver is stro ng enough to drive led displays directly. all port pins have indi- vidually selectable pull-up resistors with a suppl y-voltage invariant resistance. all i/o pins have protection diodes to both v cc and ground as indicated in figure 10-1 . refer to ?electrical char- acteristics? on page 117 for a complete list of parameters. figure 10-1. i/o pin equivalent schematic all registers and bit references in this section are written in general form. a lower case ?x? repre- sents the numbering letter for the port, and a lower case ?n? represents the bit number. however, when using the register or bit defines in a progr am, the precise form must be used. for example, portb3 for bit no. 3 in port b, here document ed generally as portxn. the physical i/o regis- ters and bit locations are listed in ?register description? on page 57 . three i/o memory address locations are allocated for each port, one each for the data register ? portx, data direction register ? ddrx, and th e port input pins ? pi nx. the port input pins i/o location is read only, while th e data register and the data direction register are read/write. however, writing a logic one to a bit in the pinx register, will result in a toggle in the correspond- ing bit in the data register. in addition, the pull-up disable ? pud bit in mcuc r disables the pull-up function for all pins in all ports when set. using the i/o port as general digital i/o is described in ?ports as general digital i/o? on page 50 . most port pins are multiplexed with alternate functions for the peripheral features on the device. how each alternate function interferes with the port pin is described in ?alternate port functions? on page 54 . refer to the individual module sectio ns for a full description of the alter- nate functions. c pin logic r p u s ee fig u re "gener a l digit a l i/o" for det a il s pxn
50 8126d?avr?11/09 attiny13a note that enabling the alternate function of some of the port pins does not affect the use of the other pins in the port as general digital i/o. 10.2 ports as general digital i/o the ports are bi-directional i/o port s with optional internal pull-ups. figure 10-2 on page 50 shows a functional description of one i/o -port pin, here generically called pxn. figure 10-2. general digital i/o (1) note: 1. wrx, wpx, wdx, rrx, rpx, and rdx are co mmon to all pins within the same port. clk i/o , sleep, and pud are common to all ports. 10.2.1 configuring the pin each port pin consists of three register bits: ddxn, portxn, and pinxn. as shown in ?register description? on page 57 , the ddxn bits are accessed at th e ddrx i/o address, the portxn bits at the portx i/o address, and the pi nxn bits at the pinx i/o address. the ddxn bit in the ddrx register selects the direct ion of this pin. if ddxn is written logic one, pxn is configured as an output pin. if ddxn is written logic ze ro, pxn is configured as an input pin. clk rpx rrx rdx wdx pud synchronizer wdx: write ddrx wrx: write portx rrx: read portx register rpx: read portx pin pud: pullup disable clk i/o : i/o clock rdx: read ddrx d l q q reset reset q q d q q d clr portxn q q d clr ddxn pinxn data b u s sleep sleep: sleep control pxn i/o wpx 0 1 wrx wpx: write pinx register
51 8126d?avr?11/09 attiny13a if portxn is written logic one w hen the pin is configured as an i nput pin, the pull-up resistor is activated. to switch the pull-up re sistor off, portxn has to be wr itten logic zero or the pin has to be configured as an output pin. the port pins are tri-stated when reset condition becomes active, even if no clocks are running. if portxn is written logic one when the pin is configured as an ou tput pin, the port pin is driven high (one). if portxn is writte n logic zero when the pin is config ured as an output pin, the port pin is driven low (zero). 10.2.2 toggling the pin writing a logic one to pinxn toggles the value of portxn, independent on the value of ddrxn. note that the sbi instruction can be us ed to toggle one single bit in a port. 10.2.3 switching between input and output when switching between tri-state ({ddxn, port xn} = 0b00) and output high ({ddxn, portxn} = 0b11), an intermediate state with either pull-up enabled {ddxn, portxn} = 0b01) or output low ({ddxn, portxn} = 0b10) must occur. norma lly, the pull-up enabled state is fully accept- able, as a high-impedant enviro nment will not notice the differenc e between a strong high driver and a pull-up. if this is not the case, the pud bit in the mcucr register can be set to disable all pull-ups in all ports. switching between input with pull-up and output low generates the same problem. the user must use either the tri-state ({ddxn, portxn} = 0b00) or the output high state ({ddxn, portxn} = 0b10) as an intermediate step. table 10-1 summarizes the control signals for the pin value. 10.2.4 reading the pin value independent of the setting of data direction bit ddxn, the port pin can be read through the pinxn register bit. as shown in figure 10-2 on page 50 , the pinxn register bit and the preced- ing latch constitute a synchronizer. this is ne eded to avoid metastability if the physical pin changes value near the edge of the internal clock, but it also introduces a delay. figure 10-3 on page 52 shows a timing diagram of the synchroni zation when reading an externally applied pin value. the maximum and minimum propagation delays are denoted t pd,max and t pd,min respectively. table 10-1. port pin configurations ddxn portxn pud (in mcucr) i/o pull-up comment 0 0 x input no tri-state (hi-z) 0 1 0 input yes pxn will source current if ext. pulled low. 0 1 1 input no tri-state (hi-z) 1 0 x output no output low (sink) 1 1 x output no output high (source)
52 8126d?avr?11/09 attiny13a figure 10-3. synchronization when reading an externally applied pin value consider the clock period starting shortly after the first falling edge of the system cl ock. the latch is closed when the clock is low, and goes transpa rent when the clock is high, as indicated by the shaded region of the ?sync latch? signal. the signal value is latched when the system clock goes low. it is clocked into the pinxn register at the succeeding positive clock edge. as indi- cated by the two arrows tpd,max and tpd,min, a single signal tr ansition on the pin will be delayed between ? and 1? system clock period depending upon the time of assertion. when reading back a software assigned pin value, a nop instruction must be inserted as indi- cated in figure 10-4 on page 52 . the out instruction sets the ?sync latch? signal at the positive edge of the clock. in this case, the delay tpd through the synchronizer is one system clock period. figure 10-4. synchronization when reading a software assigned pin value xxx in r17, pinx 0x00 0xff in s truction s s ync latch pinxn r17 xxx s y s tem clk t pd, m a x t pd, min out portx, r16 nop in r17, pinx 0xff 0x00 0xff system clk r16 instructions sync latch pinxn r17 t pd
53 8126d?avr?11/09 attiny13a the following code example shows how to set port b pins 0 and 1 high, 2 and 3 low, and define the port pins from 4 to 5 as input with a pull-up assigned to port pin 4. the resulting pin values are read back again, but as previously discussed, a nop instruction is included to be able to read back the value recently assigned to some of the pins. note: 1. for the assembly program, two temporary re gisters are used to minimize the time from pull- ups are set on pins 0, 1 and 4, unt il the direction bits are correctly set, defining bit 2 and 3 as low and redefining bits 0 and 1 as strong high drivers. note: see ?code examples? on page 6 . 10.2.5 digital input enable and sleep modes as shown in figure 10-2 on page 50 , the digital input signal can be clamped to ground at the input of the schmitt-trigger. th e signal denoted sleep in the fi gure, is set by the mcu sleep controller in power-down mode, power-save mode, and standby mode to avoid high power consumption if some input signals are left floati ng, or have an analog signal level close to v cc /2. sleep is overridden for port pins enabled as ex ternal interrupt pins. if the external interrupt request is not e nabled, sleep is active also for these pi ns. sleep is also over ridden by various other alternate functions as described in ?alternate port functions? on page 54 . assembly code example (1) ... ; define pull-ups and set outputs high ; define directions for port pins ldi r16,(1< 54 8126d?avr?11/09 attiny13a if a logic high level (?one?) is present on an asyn chronous external interrupt pin configured as ?interrupt on rising edge, falling edge, or any logic change on pin? while the external interrupt is not enabled, the corresponding external interrupt flag will be set when resuming from the above mentioned sleep mode, as the clamping in these sleep mode produces the requested logic change. 10.2.6 unconnected pins if some pins are unused, it is recommended to ensure that these pins have a defined level. even though most of the digital inputs are disabled in the deep sleep modes as described above, float- ing inputs should be avoided to reduce current consumption in all other modes where the digital inputs are enabled (reset, active mode and idle mode). the simplest method to ensure a defined level of an unused pin, is to enable the internal pull-up. in this case, the pull-up will be disabled during reset. if low po wer consumption during reset is important, it is recommended to use an extern al pull-up or pull-down. connecting unused pins directly to v cc or gnd is not recommended, since this ma y cause excessive curr ents if the pin is accidentally configured as an output. 10.3 alternate port functions most port pins have alternat e functions in addition to being general digital i/os. figure 10-5 shows how port pin control signals from the simplified figure 10-2 on page 50 can be overridden by alternate functions. figure 10-5. alternate port functions note: wrx, wpx, wdx, rrx, rpx, and rdx are co mmon to all pins within the same port. clk i/o , sleep, and pud are common to all ports. all other signals are unique for each pin. clk rpx rrx wrx rdx wdx pud synchronizer wdx: write ddrx wrx: write portx rrx: read portx register rpx: read portx pin pud: pullup disable clk i/o : i/o clock rdx: read ddrx d l q q set clr 0 1 0 1 0 1 dixn aioxn dieoexn pvovxn pvoexn ddovxn ddoexn puoexn puovxn puoexn: pxn pull-up override enable puovxn: pxn pull-up override value ddoexn: pxn data direction override enable ddovxn: pxn data direction override value pvoexn: pxn port value override enable pvovxn: pxn port value override value dixn: digital input pin n on portx aioxn: analog input/output pin n on portx reset reset q q d clr q q d clr q q d clr pinxn portxn ddxn data b u s 0 1 dieovxn sleep dieoexn: pxn digital input-enable override enable dieovxn: pxn digital input-enable override value sleep: sleep control pxn i/o 0 1 ptoexn ptoexn: pxn, port toggle override enable wpx: write pinx wpx
55 8126d?avr?11/09 attiny13a the overriding signals may not be present in all port pins, but figure 10-5 serves as a generic description applicable to all port pi ns in the avr microcontroller family. table 10-2 on page 55 summarizes the function of the overriding signals. the pin and port indexes from figure 10-5 on page 54 are not shown in the succeeding tables. the overriding signals are generated internally in the modules having the alternate function. the following subsections shortly describe the alte rnate functions for each port, and relate the overriding signals to the alternate function. refer to the alternate function description for further details. 10.3.1 alternate functions of port b the port b pins with alte rnate function are shown in table 10-3 on page 56 . table 10-2. generic description of overriding signals for alternate functions signal name full name description puoe pull-up override enable if this signal is set, the pull- up enable is controlled by the puov signal. if this signal is cleared, the pull-up is enabled when {ddxn, portxn, pud} = 0b010. puov pull-up override value if puoe is set, the pull-up is enabled/disabled when puov is set/cleared, regardless of the setting of the ddxn, portxn, and pud register bits. ddoe data direction override enable if this signal is set, the output driver enable is controlled by the ddov signal. if this signal is cleared, the output driver is enabled by the ddxn register bit. ddov data direction override value if ddoe is set, the output dr iver is enabled/disabled when ddov is set/cleared, regardless of the setting of the ddxn register bit. pvoe port value override enable if this signal is set and the output driver is enabled, the port value is controlled by the pvov signal. if pvoe is cleared, and the output driver is enabled, the port value is controlled by the portxn register bit. pvov port value override value if pvoe is set, the port value is set to pvov, regardless of the setting of the portxn register bit. ptoe port toggle override enable if ptoe is set, the portxn register bit is inverted. dieoe digital input enable override enable if this bit is set, the digital input enable is controlled by the dieov signal. if this signal is cleared, the digital input enable is determined by mcu state (normal mode, sleep mode). dieov digital input enable override value if dieoe is set, the digital i nput is enabled/disabled when dieov is set/cleared, regard less of the mcu state (normal mode, sleep mode). di digital input this is the digital input to al ternate functions. in the figure, the signal is connected to the output of the schmitt-trigger but before the synchronizer. unless the digital input is used as a clock source, the module with the alternate function will use its own synchronizer. aio analog input/output this is the analog input/output to/from alternate functions. the signal is connected directly to the pad, and can be used bi-directionally.
56 8126d?avr?11/09 attiny13a table 10-4 and table 10-5 relate the alternate functions of port b to the overriding signals shown in figure 10-5 on page 54 . note: 1. 1 when the fuse is ?0? (programmed). table 10-3. port b pins alternate functions port pin alternate function pb5 reset : reset pin dw: debugwire i/o adc0: adc input channel 0 pcint5: pin change interrupt, source 5 pb4 adc2: adc input channel 2 pcint4: pin change interrupt 0, source 4 pb3 clki: external clock input adc3: adc input channel 3 pcint3: pin change interrupt 0, source 3 pb2 sck: serial clock input adc1: adc input channel 1 t0: timer/counter0 clock source. pcint2: pin change interrupt 0, source 2 pb1 miso: spi master data in put / slave data output ain1: analog comparator, negative input oc0b: timer/counter0 compare match b output int0: external interrupt 0 input pcint1:pin change interrupt 0, source 1 pb0 mosi:: spi master data output / slave data input ain0: analog comparator, positive input oc0a: timer/counter0 compare match a output pcint0: pin change interrupt 0, source 0 table 10-4. overriding signals for alternate functions in pb5 : pb3 signal pb5/reset/adc0/p cint5 pb4/adc2/pcint4 pb3/adc3/clki/pcint3 puoe rstdisbl (1) ? dwen (1) 00 puov100 ddoe rstdisbl (1) ? dwen (1) 00 ddov debugwire transmit 0 0 pvoe 0 0 0 pvov 0 0 0 ptoe000 dieoe rstdisbl (1) + (pcint5 ? pcie + adc0d) pcint4 ? pcie + adc2d pcint3 ? pcie + adc3d dieov adc0d adc2d adc3d di pcint5 input pcint4 input pcint3 input aio reset input, adc0 input adc2 input adc3 input
57 8126d?avr?11/09 attiny13a 10.4 register description 10.4.1 mcucr ? mcu control register ? bits 7, 2 ? res: reserved bits these bits are reserved bits in the attiny13a and will always read as zero. ? bit 6 ? pud: pull-up disable when this bit is written to one, the pull-ups in the i/o ports are disabled even if the ddxn and portxn registers are configured to enable the pull-ups ({ddxn, portxn} = 0b01). see ?con- figuring the pin? on page 50 for more details about this feature. 10.4.2 portb ? port b data register 10.4.3 ddrb ? port b da ta direction register table 10-5. overriding signals for alternate functions in pb2 : pb0 signal name pb2/sck/adc1/ t0/pcint2 pb1/miso/ain1/ oc0b/int0/pcint1 pb0/mosi/ain0/ aref/oc0a/pcint0 puoe000 puov000 ddoe 0 0 0 ddov 0 0 0 pvoe 0 oc0b enable oc0a enable pvov 0 oc0b oc0a ptoe000 dieoe pcint2 ? pcie + a dc1d pcint1 ? pcie + ain1 d pcint0 ? pcie + ain0d dieov adc1d ain1d ain0d di t0/int0/ pcint2 input pcint1 input pcint0 input aio adc1 input analog comparator negative input analog comparator positive input bit 7 6 5 4 3 2 1 0 0x35 ? pud se sm1 sm0 ? isc01 isc00 mcucr read/write r r/w r/w r/w r/w r r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 0x18 ? ? portb5 portb4 portb3 portb2 portb1 portb0 portb read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 0x17 ?? ddb5 ddb4 ddb3 ddb2 ddb1 ddb0 ddrb read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
58 8126d?avr?11/09 attiny13a 10.4.4 pinb ? port b input pins address bit 76543210 0x16 ?? pinb5 pinb4 pinb3 pinb2 pinb1 pinb0 pinb read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 n/a n/a n/a n/a n/a n/a
59 8126d?avr?11/09 attiny13a 11. 8-bit timer/counter0 with pwm 11.1 features ? two independent output compare units ? double buffered output compare registers ? clear timer on compare match (auto reload) ? glitch free, phase correct pulse width modulator (pwm) ? variable pwm period ? frequency generator ? three independent interrupt sources (tov0, ocf0a, and ocf0b) 11.2 overview timer/counter0 is a general purpose 8-bit time r/counter module, with two independent output compare units, and with pwm support. it allows accurate program execution timing (event man- agement) and wave generation. a simplified block diagram of the 8-bit timer/counter is shown in figure 11-1 on page 59 . for the actual placement of i/o pins, refer to ?pinout of attiny13a? on page 2 . cpu accessible i/o registers, including i/o bits and i/o pins, are shown in bo ld. the device-specific i/o register and bit locations are listed in the ?register description? on page 70 . figure 11-1. 8-bit timer/counter block diagram clock select timer/counter data b u s ocrna ocrnb = = tcntn waveform generation waveform generation ocna ocnb = fixed top value control logic = 0 top bottom count clear direction tovn (int.req.) ocna (int.req.) ocnb (int.req.) tccrna tccrnb tn edge detector ( from prescaler ) clk tn
60 8126d?avr?11/09 attiny13a 11.2.1 registers the timer/counter (tcnt0) and output compare registers (ocr0a and ocr0b) are 8-bit registers. interrupt request (abbreviated to int.req . in the figure) signals are all visible in the timer interrupt flag register (t ifr0). all interrupts are individually masked with the timer inter- rupt mask register (timsk0). tifr0 and timsk0 are not shown in the figure. the timer/counter can be clocked internally, via th e prescaler, or by an external clock source on the t0 pin. the clock select logic block contro ls which clock source and edge the timer/counter uses to increment (or decrement) its value. th e timer/counter is inactive when no clock source is selected. the output from th e clock select logic is referred to as the timer clock (clk t0 ). the double buffered output compare registers (ocr0a and ocr0b) is compared with the timer/counter value at all times. the result of the compare can be used by the waveform gen- erator to generate a pwm or variable frequency output on the output compare pins (oc0a and oc0b). see ?output compare unit? on page 61. for details. the comp are match event will also set the compare flag (ocf0a or ocf0b) which can be used to generate an output compare interrupt request. 11.2.2 definitions many register and bit references in this section are written in general form. a lower case ?n? replaces the timer/counter number, in this case 0. a lower case ?x? replaces the output com- pare unit, in this case compare unit a or comp are unit b. however, when using the register or bit defines in a program, the precise form must be used, i.e., tcnt0 for accessing timer/counter0 counter value and so on. the definitions in table 11-1 on page 60 are also used extensivel y throughout the document. 11.3 timer/counter clock sources the timer/counter can be clocked by an internal or an external clock source. the clock source is selected by the clock select logic which is controlled by the clock select (cs02:0) bits located in the timer/counter control register (tccr0b). for details on clock sources and pres- caler, see ?timer/counter prescaler? on page 77 . 11.4 counter unit the main part of the 8-bit timer/counter is the programmable bi-directional counter unit. figure 11-2 shows a block diagram of the counter and its surroundings. table 11-1. definitions bottom the counter reaches the bottom when it becomes 0x00. max the counter reaches its maximum w hen it becomes 0xff (decimal 255). top the counter reaches the top when it becomes equal to the highest value in the count sequence. the top value can be assigned to be the fixed value 0xff (max) or the value stored in the ocr0a register. the assignment is depen- dent on the mode of operation.
61 8126d?avr?11/09 attiny13a figure 11-2. counter unit block diagram signal description (internal signals): count increment or decrement tcnt0 by 1. direction select between increment and decrement. clear clear tcnt0 (set all bits to zero). clk t n timer/counter clock, referred to as clk t0 in the following. top signalize that tcnt0 has reached maximum value. bottom signalize that tcnt0 has re ached minimum value (zero). depending of the mode of operation used, the counter is cleared, incremented, or decremented at each timer clock (clk t0 ). clk t0 can be generated from an external or internal clock source, selected by the clock select bits (cs02:0). w hen no clock source is selected (cs02:0 = 0) the timer is stopped. however, the tcnt0 value can be accessed by the cpu, regardless of whether clk t0 is present or not. a cpu write overrides (has priority over) all counter clear or count operations. the counting sequence is determined by the se tting of the wgm01 and wgm00 bits located in the timer/counter control regist er (tccr0a) and the wgm02 bit located in the timer/counter control register b (tccr0b). there are clos e connections between how the counter behaves (counts) and how waveforms are generated on the output compare output oc0a. for more details about advanced counting s equences and waveform generation, see ?modes of opera- tion? on page 64 . the timer/counter overflow flag (tov0) is set a ccording to the mode of operation selected by the wgm01:0 bits. tov0 can be us ed for generating a cpu interrupt. 11.5 output compare unit the 8-bit comparator continuously compares tcnt0 with the output compare registers (ocr0a and ocr0b). whenever tcnt0 equals ocr0 a or ocr0b, the comparator signals a match. a match will set the output compare flag (ocf0a or ocf0 b) at the next timer clock cycle. if the corresponding interrupt is enabled, the output compare flag generates an output compare interrupt. the output compare flag is aut omatically cleared when the interrupt is exe- cuted. alternatively, the flag can be cleared by software by writing a logical one to its i/o bit location. the waveform generator uses the matc h signal to generate an output according to operating mode set by the wgm02:0 bits and compare output mode (com0x1:0) bits. the max and bottom signals are used by the waveform generator for handling the special cases of the extreme values in some modes of operation ( see ?modes of operation? on page 64. ). figure 11-3 on page 62 shows a block diagram of the output compare unit. data b u s tcntn control logic count tovn (int.req.) clock select top tn edge detector ( from prescaler ) clk tn bottom direction clear
62 8126d?avr?11/09 attiny13a figure 11-3. output compare unit, block diagram the ocr0x registers are double buffered when using any of the pulse width modulation (pwm) modes. for the normal and clear timer on compare (ctc) modes of operation, the dou- ble buffering is disabled. the double buffering synchronizes the update of the ocr0x compare registers to either top or bottom of the counting sequence. the synchronization prevents the occurrence of odd-length, non-symmetrical pwm pulses, thereby making the output glitch-free. the ocr0x register access may seem complex, but this is not case. when the double buffering is enabled, the cpu has access to the ocr0x buffer register, and if double buffering is dis- abled the cpu will access the ocr0x directly. 11.5.1 force output compare in non-pwm waveform generation modes, the matc h output of the comparator can be forced by writing a one to the force outp ut compare (foc0x) bit. forcin g compare match will not set the ocf0x flag or reload/clear the timer, but the oc0x pin will be updated as if a real compare match had occurred (the com0x1:0 bits settings de fine whether the oc0x pin is set, cleared or toggled). 11.5.2 compare match blocking by tcnt0 write all cpu write operations to the tcnt0 register will block any compare ma tch that occur in the next timer clock cycle, even when the timer is stopped. this feat ure allows ocr0x to be initial- ized to the same value as tcnt0 without trigge ring an interrupt when the timer/counter clock is enabled. 11.5.3 using the output compare unit since writing tcnt0 in any mo de of operation will block all compare matches for one timer clock cycle, there are risks involved when ch anging tcnt0 when using the output compare unit, independently of whether the timer/counter is running or not. if the value written to tcnt0 equals the ocr0x value, the compare match will be missed, resulting in incorrect waveform ocfn x (int.req.) = (8-bit comparator ) ocrnx ocnx data b u s tcntn wgmn1:0 waveform generator top focn comnx1:0 bottom
63 8126d?avr?11/09 attiny13a generation. similarly, do not write the tcnt0 value equal to bottom when the counter is down-counting. the setup of the oc0x should be performed before setting the data direction register for the port pin to output. the easiest way of setting the oc0x value is to use the force output com- pare (foc0x) strobe bits in normal mode. the oc0x registers keep their values even when changing between waveform generation modes. be aware that the com0x1:0 bits are not doubl e buffered together with the compare value. changing the com0x1:0 bits will take effect immediately. 11.6 compare match output unit the compare output mode (com0x1:0) bits ha ve two functions. the waveform generator uses the com0x1:0 bits for defining the output co mpare (oc0x) state at the next compare match. also, the com0x1:0 bits contro l the oc0x pin output source. figure 11-4 on page 63 shows a simplified schematic of the logic affected by th e com0x1:0 bit setting. the i/o registers, i/o bits, and i/o pins in the figure are shown in bold. only the parts of the general i/o port control registers (ddr and port) that are affected by the com0x1:0 bits are shown. when referring to the oc0x state, the reference is for the inter nal oc0x register, not the oc0x pin. if a system reset occur, the oc0x register is reset to ?0?. figure 11-4. compare match output unit, schematic the general i/o port function is overridden by the output compare (oc0x) from the waveform generator if either of the com0x1:0 bits are se t. however, the oc0x pin direction (input or out- put) is still controlled by the da ta direction register (ddr) for th e port pin. the data direction register bit for the oc0x pin (ddr_oc0x) must be set as output before th e oc0x value is visi- ble on the pin. the port over ride function is independent of the waveform generation mode. port ddr dq dq ocn pin ocnx dq w a veform gener a tor comnx1 comnx0 0 1 data b u s focn clk i/o
64 8126d?avr?11/09 attiny13a the design of the output compare pin logic allows initialization of the oc 0x state before the out- put is enabled. note that some com0x1:0 bi t settings are reserved for certain modes of operation. see ?register description? on page 70. 11.6.1 compare output mode and waveform generation the waveform generator uses the com0x1:0 bits differently in normal, ctc, and pwm modes. for all modes, setting the com0x1:0 = 0 tell s the waveform generator that no action on the oc0x register is to be perfo rmed on the next compare match. for compare output actions in the non-pwm modes refer to table 11-2 on page 70 . for fast pwm mode, refer to table 11-3 on page 71 , and for phase correct pwm refer to table 11-4 on page 71 . a change of the com0x1:0 bits state will have effe ct at the first compare match after the bits are written. for non-pwm modes, the action can be fo rced to have immediate effect by using the foc0x strobe bits. 11.7 modes of operation the mode of operation, i.e., t he behavior of the timer/counter and the output compare pins, is defined by the combination of the waveform generation mode (wgm02:0) and compare output mode (com0x1:0) bits. the compare output mode bits do not affect the counting sequence, while the waveform generation mode bits do. the com0x1:0 bits control whether the pwm out- put generated should be inverted or not (inverted or non-inverted pwm). for non-pwm modes the com0x1:0 bits control whether the output should be set, cleared, or toggled at a compare match ( see ?compare match output unit? on page 63. ). for detailed timing information refer to figure 11-8 on page 69 , figure 11-9 on page 69 , figure 11-10 on page 69 and figure 11-11 on page 70 in ?timer/counter timing diagrams? on page 68 . 11.7.1 normal mode the simplest mode of operation is the normal mode (wgm02:0 = 0). in this mode the counting direction is always up (incre menting), and no counter clear is performed. the counter simply overruns when it passes its maxi mum 8-bit value (top = 0xff) and then restarts from the bot- tom (0x00). in normal o peration the timer/counter overflow flag (tov0) will be set in the same timer clock cycle as the tcnt0 becomes zero. the tov0 flag in this case behaves like a ninth bit, except that it is only set, not cleared. ho wever, combined with the timer overflow interrupt that automatically clears the tov0 flag, the timer resolution can be increased by software. there are no special cases to consider in the normal mode, a new counter value can be written anytime. the output compare unit can be used to generate interrupts at some given time. using the out- put compare to generate waveforms in normal mode is not recommended, since this will occupy too much of the cpu time. 11.7.2 clear timer on compare match (ctc) mode in clear timer on compare or ctc mode (wgm 02:0 = 2), the ocr0a register is used to manipulate the counter resolution . in ctc mode the counter is cleared to zero when the counter value (tcnt0) matches the ocr0a. the ocr0a de fines the top value fo r the counter, hence also its resolution. this mode allows greater control of the compare match output frequency. it also simplifies the operation of counting external events.
65 8126d?avr?11/09 attiny13a the timing diagram for the ctc mode is shown in figure 11-5 on page 65 . the counter value (tcnt0) increases until a compare match occurs between tcnt0 and ocr0a, and then coun- ter (tcnt0) is cleared. figure 11-5. ctc mode, timing diagram an interrupt can be generated each time the c ounter value reaches the top value by using the ocf0a flag. if the interrupt is enabled, the interrupt handler routine can be used for updating the top value. however, changing top to a va lue close to bottom when the counter is run- ning with none or a low prescaler value must be done with care since the ctc mode does not have the double buffering feature. if the new value written to ocr0a is lower than the current value of tcnt0, the counter will miss the compar e match. the counter will then have to count to its maximum value (0xff) and wrap around starting at 0x00 before the compare match can occur. for generating a waveform output in ctc mode, t he oc0a output can be set to toggle its logical level on each compare match by setting the compare output mode bits to toggle mode (com0a1:0 = 1). the oc0a value will not be visible on the port pin unless the data direction for the pin is set to output. the waveform ge nerated will have a ma ximum frequency of f oc0 = f clk_i/o /2 when ocr0a is set to zero (0x00). the waveform frequency is defined by the following equation: the n variable represents the prescale factor (1, 8, 64, 256, or 1024). as for the normal mode of operat ion, the tov0 flag is set in the same timer clock cycle that the counter counts fr om max to 0x00. 11.7.3 fast pwm mode the fast pulse width modulation or fast pwm mode (wgm02:0 = 3 or 7) provides a high fre- quency pwm waveform generation option. the fa st pwm differs from the other pwm option by its single-slope operation. the c ounter counts from bottom to top then restarts from bot- tom. top is defined as 0xff when wgm2:0 = 3, and ocr0a when wgm2:0 = 7. in non- inverting compare output mode, the output compare (oc0x) is cleared on the compare match between tcnt0 and ocr0x, and set at bottom. in inverting compare output mode, the out- put is set on compare match and cleared at bottom. due to the single-slope operation, the operating frequency of the fast pwm mode can be twice as high as the phase correct pwm mode that use dual-slope operation. this high frequency makes the fast pwm mode well suited tcntn ocn (toggle) ocnx interrupt flag set 1 4 period 2 3 (comnx1:0 = 1) f ocnx f clk_i/o 2 n 1 ocrnx + () ?? -------------------------------------------------- =
66 8126d?avr?11/09 attiny13a for power regulation, rectification, and dac app lications. high frequency a llows physically small sized external components (coils, capacitors), and therefore reduces total system cost. in fast pwm mode, the counter is incremented until the counter value matches the top value. the counter is then cleared at the following timer clock cycle. the timing diagram for the fast pwm mode is shown in figure 11-6 on page 66 . the tcnt0 value is in the timing diagram shown as a histogram for illustrating the singl e-slope operation. the diagram includes non- inverted and inverted pwm outputs. the small hor izontal line marks on t he tcnt0 slopes repre- sent compare matches between ocr0x and tcnt0. figure 11-6. fast pwm mode, timing diagram the timer/counter overflow flag (tov0) is set each time the counter re aches top. if the inter- rupt is enabled, the interrupt handler routi ne can be used for updating the compare value. in fast pwm mode, the compare unit allows generation of pwm waveforms on the oc0x pins. setting the com0x1:0 bits to two will produce a non-inverted pwm and an inverted pwm output can be generated by setting the com0x1:0 to th ree: setting the com0a1:0 bits to one allows the ac0a pin to toggle on compare matches if t he wgm02 bit is set. this option is not available for the oc0b pin (see table 11-3 on page 71 ). the actual oc0x value will only be visible on the port pin if the data direction fo r the port pin is set as output. the pwm waveform is generated by setting (or clearing) the oc0x register at the compare match between ocr0x and tcnt0, and clearing (or setting) the oc0x r egister at the timer clock c ycle the counter is cleared (changes from top to bottom). the pwm frequency for the output can be calculated by the following equation: the n variable represents the prescale factor (1, 8, 64, 256, or 1024). the extreme values for the ocr0a register represents special cases when generating a pwm waveform output in the fast pwm mode. if t he ocr0a is set equal to bottom, the output will be a narrow spike for each max+1 timer clock cycle. setting the ocr0a equal to max will result tcntn ocrnx update and tovn interrupt flag set 1 period 2 3 ocn ocn (comnx1:0 = 2) (comnx1:0 = 3) ocrnx interrupt flag set 4 5 6 7 f ocnxpwm f clk_i/o n 256 ? ------------------ =
67 8126d?avr?11/09 attiny13a in a constantly high or low output (depending on the polarity of the out put set by the com0a1:0 bits.) a frequency (with 50% duty cycle) waveform out put in fast pwm mode can be achieved by set- ting oc0x to toggle its logical level on each compare match (com0x1:0 = 1). the waveform generated will have a maximum frequency of f oc0 = f clk_i/o /2 when ocr0a is set to zero. this feature is similar to the oc0a toggle in ctc mo de, except the double buff er feature of the out- put compare unit is enabled in the fast pwm mode. 11.7.4 phase correct pwm mode the phase correct pwm mode (wgm02:0 = 1 or 5) provides a high resolution phase correct pwm waveform generation option. the phase correct pwm mode is based on a dual-slope operation. the counter counts repeatedly from bottom to top and then from top to bot- tom. top is defined as 0xff when wgm2:0 = 1, and ocr0a when wgm2:0 = 5. in non- inverting compare output mode, the output compare (oc0x) is cleared on the compare match between tcnt0 and ocr0x while upcounting, and set on the compare match while down- counting. in inverting output compare mode, the operation is inverted. the dual-slope operation has lower maximum operation frequency than single slope operation. however, due to the sym- metric feature of the dual-slope pwm modes, t hese modes are preferred for motor control applications. in phase correct pwm mode the counter is incremented until the counter value matches top. when the counter reaches top, it changes the count direction. the tcnt0 value will be equal to top for one timer clock cycle. the timing diagram for the phase correct pwm mode is shown on figure 11-7 on page 67 . the tcnt0 value is in the timing diagram shown as a histogram for illustrating the dual-sl ope operation. the diagram includes non-invert ed and inverted pwm out- puts. the small horizontal line marks on the tcnt0 slopes represent compare matches between ocr0x and tcnt0. figure 11-7. phase correct pwm mode, timing diagram tovn interr u pt fl a g s et ocnx interr u pt fl a g s et 1 2 3 tcntn period ocn ocn (comnx1:0 = 2) (comnx1:0 = 3 ) ocrnx upd a te
68 8126d?avr?11/09 attiny13a the timer/counter overflow flag (tov0) is set each time the counter reaches bottom. the interrupt flag can be used to g enerate an interrupt each time the counter reaches the bottom value. in phase correct pwm mode, the compare unit allows generation of pwm waveforms on the oc0x pins. setting the com0x1:0 bits to two will produce a non-inverted pwm. an inverted pwm output can be generated by setting the com0x1:0 to three: setting the com0a0 bits to one allows the oc0a pin to toggle on compare ma tches if the wgm02 bit is set. this option is not available for the oc0b pin (see table 11-4 on page 71 ). the actual oc0x value will only be visible on the port pin if the data direction for the port pin is set as output. the pwm waveform is generated by clearing (or setting) the oc0x register at the compare match between ocr0x and tcnt0 when the counter increments, and setti ng (or clearing) the oc0x register at com- pare match between ocr0x and tcnt0 when the counter decrements. the pwm frequency for the output when using phase correct pwm c an be calculated by the following equation: the n variable represents the prescale factor (1, 8, 64, 256, or 1024). the extreme values for the ocr0a register represent special cases when generating a pwm waveform output in the phase correct pwm mo de. if the ocr0a is set equal to bottom, the output will be continuously low an d if set equal to max the output will be continuously high for non-inverted pwm mode. for in verted pwm the output will have the opposite logic values. at the very start of period 2 in figure 11-7 on page 67 ocn has a transition from high to low even though there is no compare match. the poin t of this transition is to guarantee symmetry around bottom. there are two cases that gi ve a transition without compare match. ? ocr0a changes its valu e from max, like in figure 11-7 on page 67 . when the ocr0a value is max the ocn pin value is the same as the result of a down-counting compare match. to ensure symmetry around bottom the ocn value at max must correspond to the result of an up-counting compare match. ? the timer starts counting from a value higher than the one in ocr0a, and for that reason misses the compare match and hence the ocn change that would have happened on the way up. 11.8 timer/counter timing diagrams the timer/counter is a synchronous design and the timer clock (clk t0 ) is therefore shown as a clock enable signal in the following figures. the figures include information on when interrupt flags are set. figure 11-8 on page 69 contains timing data for basic timer/counter operation. the figure shows the count sequence close to the max value in all modes other than phase cor- rect pwm mode. f ocnxpcpwm f clk_i/o n510 ? ------------------ =
69 8126d?avr?11/09 attiny13a figure 11-8. timer/counter timing diagram, no prescaling figure 11-9 shows the same timing data, but with the prescaler enabled. figure 11-9. timer/counter timing diagram, with prescaler (f clk_i/o /8) figure 11-10 shows the setting of ocf0b in all mo des and ocf0a in all modes except ctc mode and pwm mode, where ocr0a is top. figure 11-10. timer/counter timing diagram, setting of ocf0x, with prescaler (f clk_i/o /8) clk tn (clk i/o /1) tovn clk i/o tcntn max - 1 max bottom bottom + 1 tovn tcntn max - 1 max bottom bottom + 1 clk i/o clk tn (clk i/o / 8 ) ocfnx ocrnx tcntn ocrnx v a l u e ocrnx - 1 ocrnx ocrnx + 1 ocrnx + 2 clk i/o clk tn (clk i/o / 8 )
70 8126d?avr?11/09 attiny13a figure 11-11 shows the setting of ocf0a and the clearing of tcnt0 in ctc mode and fast pwm mode where ocr0a is top. figure 11-11. timer/counter timing diagram, clear timer on compare match mode, with pres- caler (f clk_i/o /8) 11.9 register description 11.9.1 tccr0a ? timer/coun ter control register a ? bits 7:6 ? com01a:0: compare match output a mode these bits control the output compare pin (oc0a) behavior. if one or both of the com0a1:0 bits are set, the oc0a output overrides the normal po rt functionality of the i/o pin it is connected to. however, note that the data direction r egister (ddr) bit corresponding to the oc0a pin must be set in order to enable the output driver. when oc0a is connected to the pin, the function of the com0a1:0 bits depends on the wgm02:0 bit setting. table 11-2 shows the com0a1:0 bit functionality when the wgm02:0 bits are set to a normal or ctc mode (non-pwm). ocfnx ocrnx tcntn (ctc) top top - 1 top bottom bottom + 1 clk i/o clk tn (clk i/o / 8 ) bit 7 6 5 4 3 210 0x2f com0a1 com0a0 com0b1 com0b0 ? ? wgm01 wgm00 tccr0a read/write r/w r/w r/w r/w r r r/w r/w initial value 0 0 0 0 0 0 0 0 table 11-2. compare output mode, non-pwm mode com0a1 com0a0 description 0 0 normal port operation, oc0a disconnected. 0 1 toggle oc0a on compare match 1 0 clear oc0a on compare match 1 1 set oc0a on compare match
71 8126d?avr?11/09 attiny13a table 11-3 shows the com0a1:0 bit functionality when the wgm01:0 bits are set to fast pwm mode. note: 1. a special case occurs when ocr0a equals top and com0a1 is set. in this case, the com- pare match is ignored, but the se t or clear is done at top. see ?fast pwm mode? on page 65 for more details. table 11-4 shows the com0a1:0 bit functionality when the wgm02:0 bits are set to phase cor- rect pwm mode. note: 1. a special case occurs when ocr0a equals top and com0a1 is set. in this case, the com- pare match is ignored, but the set or clear is done at top. see ?phase correct pwm mode? on page 67 for more details. ? bits 5:4 ? com0b1:0: compare match output b mode these bits control the output compare pin (oc0b) behavior. if one or both of the com0b1:0 bits are set, the oc0b output overrides the normal po rt functionality of the i/o pin it is connected to. however, note that the data direction r egister (ddr) bit corresponding to the oc0b pin must be set in order to enable the output driver. when oc0b is connected to the pin, the function of the com0b1:0 bits depends on the wgm02:0 bit setting. table 11-5 on page 72 shows the com0b1:0 bit functionalit y when the wgm02:0 bits are set to a normal or ctc mode (non-pwm). table 11-3. compare output mode, fast pwm mode (1) com0a1 com0a0 description 0 0 normal port operation, oc0a disconnected. 01 wgm02 = 0: normal port op eration, oc0a disconnected. wgm02 = 1: toggle oc0a on compare match. 1 0 clear oc0a on compare match, set oc0a at top 1 1 set oc0a on compare match, clear oc0a at top table 11-4. compare output mode, phase correct pwm mode (1) com0a1 com0a0 description 0 0 normal port operation, oc0a disconnected. 01 wgm02 = 0: normal port op eration, oc0a disconnected. wgm02 = 1: toggle oc0a on compare match. 10 clear oc0a on compare match when up-counting. set oc0a on compare match when down-counting. 11 set oc0a on compare match when up-counting. clear oc0a on compare match when down-counting.
72 8126d?avr?11/09 attiny13a table 11-6 shows the com0b1:0 bit functionality when the wgm02:0 bits are set to fast pwm mode. note: 1. a special case occurs when ocr0b equals top and com0b1 is set. in this case, the com- pare match is ignored, but the se t or clear is done at top. see ?fast pwm mode? on page 65 for more details. table 11-7 shows the com0b1:0 bit functionality when the wgm02:0 bits are set to phase cor- rect pwm mode. note: 1. a special case occurs when ocr0b equals top and com0b1 is set. in this case, the com- pare match is ignored, but the set or clear is done at top. see ?phase correct pwm mode? on page 67 for more details. ? bits 3, 2 ? res: reserved bits these bits are reserved bits in the attiny13a and will always read as zero. table 11-5. compare output mode, non-pwm mode com0b1 com0b0 description 0 0 normal port operation, oc0b disconnected. 0 1 toggle oc0b on compare match 1 0 clear oc0b on compare match 1 1 set oc0b on compare match table 11-6. compare output mode, fast pwm mode (1) com0b1 com0b0 description 0 0 normal port operation, oc0b disconnected. 01reserved 1 0 clear oc0b on compare match, set oc0b at top 1 1 set oc0b on compare match, clear oc0b at top table 11-7. compare output mode, phase correct pwm mode (1) com0b1 com0b0 description 0 0 normal port operation, oc0b disconnected. 01reserved 10 clear oc0b on compare match when up-counting. set oc0b on compare match when down-counting. 11 set oc0b on compare match when up-counting. clear oc0b on compare match when down-counting.
73 8126d?avr?11/09 attiny13a ? bits 1:0 ? wgm01:0: waveform generation mode combined with the wgm02 bit found in the tccr0b register, these bits control the counting sequence of the counter, the source for maximu m (top) counter value, and what type of wave- form generation to be used, see table 11-8 on page 73 . modes of operation supported by the timer/counter unit are: normal mode (counte r), clear timer on compare match (ctc) mode, and two types of pulse width modulation (pwm) modes (see ?modes of operation? on page 64 ). notes: 1. max = 0xff 2. bottom = 0x00 11.9.2 tccr0b ? timer/coun ter control register b ? bit 7 ? foc0a: force output compare a the foc0a bit is only active when the wgm bits specify a non-pwm mode. however, for ensuring compatibility with future devices, this bit must be set to zero when tccr0b is written when operating in pwm mode. when writing a logical one to the foc0a bit, an immediate compare match is forced on the wa veform generation unit . the oc0a output is changed according to its com0a1:0 bits setting. note that the foc0a bit is implemented as a strobe. therefore it is the value present in the com0a1:0 bits that determines the effect of the forced compare. a foc0a strobe will not generate any interrupt, nor will it clear the timer in ctc mode using ocr0a as top. the foc0a bit is always read as zero. table 11-8. waveform generation mode bit description mode wgm2 wgm1 wgm0 timer/counter mode of operation top update of ocrx at tov flag set on (1)(2) 0 0 0 0 normal 0xff immediate max 1001 pwm (phase correct) 0xff top bottom 2010ctc ocraimmediatemax 3 0 1 1 fast pwm 0xff top max 4100reserved ? ? ? 5101 pwm (phase correct) ocra top bottom 6110reserved ? ? ? 7111fast pwm ocratop top bit 7 6 5 4 3 210 0x33 foc0a foc0b ? ? wgm02 cs02 cs01 cs00 tccr0b read/write w w r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
74 8126d?avr?11/09 attiny13a ? bit 6 ? foc0b: force output compare b the foc0b bit is only active when the wgm bits specify a non-pwm mode. however, for ensuring compatibility with future devices, this bit must be set to zero when tccr0b is written when operating in pwm mode. when writing a logical one to the foc0b bit, an immediate compare match is forced on the wa veform generation unit . the oc0b output is changed according to its com0b1:0 bits setting. note that the foc0b bit is implemented as a strobe. therefore it is the value present in the com0b1:0 bits that determines the effect of the forced compare. a foc0b strobe will not generate any interrupt, nor will it clear the timer in ctc mode using ocr0b as top. the foc0b bit is always read as zero. ? bits 5:4 ? res: reserved bits these bits are reserved bits in the attiny13a and will always read as zero. ? bit 3 ? wgm02: waveform generation mode see the description in the ?tccr0a ? timer/counter control register a? on page 70 . ? bits 2:0 ? cs02:0: clock select the three clock select bits select the cloc k source to be used by the timer/counter. if external pin modes are used for the timer/counter0, transitions on the t0 pin will clock the counter even if the pin is configured as an outpu t. this feature allows software control of the counting. 11.9.3 tcnt0 ? time r/counter register the timer/counter register gives direct ac cess, both for read and write operations, to the timer/counter unit 8-bit counter. writing to the tcnt0 register blocks (removes) the compare match on the following timer clock. modifying t he counter (tcnt0) while the counter is running, introduces a risk of missing a compare match between tcnt0 and the ocr0x registers. table 11-9. clock select bit description cs02 cs01 cs00 description 0 0 0 no clock source (timer/counter stopped) 001clk i/o /(no prescaling) 010clk i/o /8 (from prescaler) 011clk i/o /64 (from prescaler) 100clk i/o /256 (from prescaler) 101clk i/o /1024 (from prescaler) 1 1 0 external clock source on t0 pin. clock on falling edge. 1 1 1 external clock source on t0 pin. clock on rising edge. bit 76543210 0x32 tcnt0[7:0] tcnt0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
75 8126d?avr?11/09 attiny13a 11.9.4 ocr0a ? output compare register a the output compare register a contains an 8-bi t value that is conti nuously compared with the counter value (tcnt0). a match can be used to generate an output compare interrupt, or to generate a waveform output on the oc0a pin. 11.9.5 ocr0b ? output compare register b the output compare register b contains an 8-bi t value that is conti nuously compared with the counter value (tcnt0). a match can be used to generate an output compare interrupt, or to generate a waveform output on the oc0b pin. 11.9.6 timsk0 ? timer/counter interrupt mask register ? bits 7:4, 0 ? res: reserved bits these bits are reserved bits in the attiny13a and will always read as zero. ? bit 3 ? ocie0b: timer/counter output compare match b interrupt enable when the ocie0b bit is written to one, and the i-bit in the status register is set, the timer/counter compare match b interrupt is enab led. the corresponding interrupt is executed if a compare match in timer/counter occurs, i.e., when the ocf0b bi t is set in the timer/counter interrupt flag register ? tifr0. ? bit 2 ? ocie0a: timer/counter0 output compare match a interrupt enable when the ocie0a bit is written to one, and th e i-bit in the status register is set, the timer/counter0 compare match a interrupt is enabled. the corresponding interrupt is executed if a compare match in timer/counter0 occurs, i.e., when the ocf0a bit is set in the timer/counter 0 interrupt flag register ? tifr0. ? bit 1 ? toie0: timer/counter0 overflow interrupt enable when the toie0 bit is written to one, and t he i-bit in the status register is set, the timer/counter0 overflow interr upt is enabled. the corresponding interrupt is executed if an overflow in timer/counter0 occu rs, i.e., when the tov0 bit is set in the timer/counter 0 inter- rupt flag register ? tifr0. bit 76543210 0x36 ocr0a[7:0] ocr0a read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 0x29 ocr0b[7:0] ocr0b read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543 2 10 0x39 ????ocie0bocie0atoie0?timsk0 read/writerrrrr/wr/wr/wr initial value00000 0 00
76 8126d?avr?11/09 attiny13a 11.9.7 tifr0 ? timer/counter 0 interrupt flag register ? bits 7:4, 0 ? res: reserved bits these bits are reserved bits in the attiny13a and will always read as zero. ? bit 3 ? ocf0b: output compare flag 0 b the ocf0b bit is set when a compare match occurs between the timer/counter and the data in ocr0b ? output compare register0 b. ocf0b is cleared by hardware when executing the cor- responding interrupt handling vector. alternatively, ocf0b is cleared by writing a logic one to the flag. when the i-bit in sreg, ocie0b (tim er/counter compare b match interrupt enable), and ocf0b are set, the timer/counter compare match interrupt is executed. ? bit 2 ? ocf0a: output compare flag 0 a the ocf0a bit is set when a compare match oc curs between the timer/counter0 and the data in ocr0a ? output compare register0. ocf0a is cleared by hardware when executing the cor- responding interrupt handling vector. alternativel y, ocf0a is cleared by writing a logic one to the flag. when the i-bit in sreg, ocie0a (t imer/counter0 compare match interrupt enable), and ocf0a are set, the timer/counter0 co mpare match interrupt is executed. ? bit 1 ? tov0: timer/counter0 overflow flag the bit tov0 is set when an overflow occurs in timer/counter0. tov0 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, tov0 is cleared by writing a logic one to the flag. when the sreg i-bit, toie0 (timer/count er0 overflow interrupt enable), and tov0 are set, the timer/co unter0 overflow interrupt is executed. the setting of this flag is dependent of the wgm02:0 bit setting. refer to table 11-8 , ?waveform generation mode bit description? on page 73 . bit 76543210 0x38 ? ? ? ? ocf0b ocf0a tov0 ?tifr0 read/write r r r r r/w r/w r/w r initial value00000000
77 8126d?avr?11/09 attiny13a 12. timer/counter prescaler 12.1 overview the timer/counter can be clocked directly by th e system clock (by setting the csn2:0 = 1). this provides the fastest operation, with a maximum timer/counter clock frequency equal to system clock frequency (f clk_i/o ). alternatively, one of four taps from the prescaler can be used as a clock source. the prescaled clock has a frequency of either f clk_i/o /8, f clk_i/o /64, f clk_i/o /256, or f clk_i/o /1024. 12.2 prescaler reset the prescaler is free running, i.e., operates independently of the clock select logic of the timer/counter. since the prescaler is not affected by the timer/counter?s clock select, the state of the prescaler will have implicati ons for situations w here a prescaled clock is used. one exam- ple of prescaling artifacts occurs when the timer is enabled and clocked by the prescaler (6 > csn2:0 > 1). the number of system clock cycles from when the time r is enabled to the first count occurs can be from 1 to n+1 system clock cycles , where n equals the pre scaler divisor (8, 64, 256, or 1024). it is possible to use the prescaler reset for synchronizing the timer/counter to program execution. 12.3 external clock source an external clock source app lied to the t0 pin can be used as timer/counter clock (clk t0 ). the t0 pin is sampled once every system clock cycle by the pin synchronization logic. the synchro- nized (sampled) signal is then passed through the edge detector. figure 12-1 on page 77 shows a functional equivalent block diagram of the t0 synchronization and edge detector logic. the registers are clocked at the positive edge of the internal system clock ( clk i/o ). the latch is trans- parent in the high period of the internal system clock. the edge detector generates one clk t 0 pulse for each positive (csn2:0 = 7) or negative (csn2:0 = 6) edge it detects. figure 12-1. t0 pin sampling the synchronization and e dge detector logic introduces a de lay of 2.5 to 3.5 system clock cycles from an edge has been applied to the t0 pin to the counter is updated. enabling and disabling of the clock input must be done when t0 has been stable for at least one system clock cycle, otherwise it is a risk that a false timer/counter clock pulse is generated. each half period of the exter nal clock applied must be longer than one system clock cycle to ensure correct sampling. the external clock must be guaranteed to have less than half the sys- tem clock frequency (f extclk < f clk_i/o /2) given a 50/50% duty cycle. since the edge detector uses tn_sync (to clock select logic) edge detector synchronization dq dq le dq tn clk i/o
78 8126d?avr?11/09 attiny13a sampling, the maximum frequency of an external clock it can detect is half the sampling fre- quency (nyquist sampling theorem). however, due to variation of the system clock frequency and duty cycle caused by oscillator source (crystal, resonator, and capacitors) tolerances, it is recommended that maximum frequency of an external clock source is less than f clk_i/o /2.5. an external clock source can not be prescaled. figure 12-2. prescaler for timer/counter0 note: 1. the synchronization logic on the input pins ( t0) is shown in figure 12-1 on page 77 . 12.4 register description. 12.4.1 gtccr ? general timer/counter control register ? bit 7 ? tsm: timer/counter synchronization mode writing the tsm bit to one activa tes the timer/counter synchroniz ation mode. in this mode, the value that is written to the psr10 bit is kept, hence keeping the prescaler reset signal asserted. this ensures that the timer/counter is halted an d can be configured without the risk of advanc- ing during configuration. when t he tsm bit is written to zero , the psr10 bit is cleared by hardware, and the timer/ counter start counting. ? bit 0 ? psr10: prescaler reset timer/counter0 when this bit is one, the timer/ counter0 prescaler will be reset. this bit is normally cleared immediately by hardware, except if the tsm bit is set. psr10 clear clk t0 t0 clk i/o synchronization bit 7 6 5 4 3 2 1 0 0x28 tsm ? ? ? ? ? ? psr10 gtccr read/write r/w r r r r r r r/w initial value 0 0 0 0 0 0 0 0
79 8126d?avr?11/09 attiny13a 13. analog comparator the analog comparator compares the input va lues on the positive pin ain0 and negative pin ain1. when the voltage on the positive pin ain0 is higher than the voltage on the negative pin ain1, the analog comparator output, aco, is se t. the comparator can trigger a separate inter- rupt, exclusive to the analog comparator. the user can select interrupt triggering on comparator output rise, fall or toggle. a block diagram of t he comparator and its surrounding logic is shown in figure 13-1 on page 79 . figure 13-1. analog comparator block diagram see figure 1-1 on page 2 , table 10-5 on page 57 , and table 13-2 on page 81 for analog compar- ator pin placement. 13.1 analog comparator multiplexed input it is possible to select any of the adc3..0 pins to replace the negative input to the analog com- parator. the adc multiplexer is used to select this input, and conseq uently, the adc must be switched off to utilize this feature. if the analog comparator multiplexer enable bit (acme in adcsrb) is set and the adc is switched off (ade n in adcsra is zero), mux1:0 in admux select the input pin to replace the negative input to the analog comparator, as shown in table 13-1 . if acme is cleared or aden is set, ain1 is applied to the negative input to the analog comparator. acbg bandgap reference adc multiplexer output acme aden (1) table 13-1. analog comparator multiplexed input acme aden mux1..0 analog comparator negative input 0x xxain1 11 xxain1 1 0 00 adc0 1 0 01 adc1 1 0 10 adc2 1 0 11 adc3
80 8126d?avr?11/09 attiny13a 13.2 register description 13.2.1 adcsrb ? adc contro l and status register ? bit 6 ? acme: analog comparator multiplexer enable when this bit is written logic one and the adc is switched off (aden in adcsra is zero), the adc multiplexer selects the negative input to the analog comparator. when this bit is written logic zero, ain1 is applied to the negative input of the analog comparator. for a detailed description of this bit, see ?analog comparator multiplexed input? on page 79 . 13.2.2 acsr? analog comparator control and status register ? bit 7 ? acd: analog comparator disable when this bit is written logic one, the power to the analog comparator is switched off. this bit can be set at any time to tu rn off the analog com parator. this will reduce power consumption in active and idle mode. when changing the acd bit, the analog comparator interrupt must be disabled by clearing the acie bit in acsr. ot herwise an interrupt can occur when the bit is changed. ? bit 6 ? acbg: analog comparator bandgap select when this bit is set, a fixed bandgap reference vo ltage replaces the positive input to the analog comparator. when this bit is clea red, ain0 is applied to the positive input of the analog compar- ator. when the ba ndgap reference is used as input to the analog comparator, it will take certain time for the voltage to stabilize. if not stabilized, the fi rst value may give a wrong value. ? bit 5 ? aco: analog comparator output the output of the analog comparator is synchron ized and then directly connected to aco. the synchronization introduces a delay of 1 - 2 clock cycles. ? bit 4 ? aci: analog comparator interrupt flag this bit is set by hardware when a comparator output event triggers t he interrupt mode defined by acis1 and acis0. the analog comparator interr upt routine is executed if the acie bit is set and the i-bit in sreg is set. aci is cleared by hardware when executing the corresponding inter- rupt handling vector. alternatively, aci is cleared by writing a logic one to the flag. ? bit 3 ? acie: analog comparator interrupt enable when the acie bit is written logic one and the i-bi t in the status register is set, the analog com- parator interrupt is activated. when writ ten logic zero, the interrupt is disabled. ? bit 2 ? res: reserved bit this bit is a reserved bit in the at tiny13a and will always read as zero. bit 7 6543210 0x03 ? acme ? ? ? adts2 adts1 adts0 adcsrb read/write r r/w r r r r/w r/w r/w initial value0 0000000 bit 76543210 0x08 acd acbg aco aci acie ? acis1 acis0 acsr read/write r/w r/w r r/w r/w r r/w r/w initial value00n/a00000
81 8126d?avr?11/09 attiny13a ? bits 1, 0 ? acis1, acis0: analog comparator interrupt mode select these bits determine which compar ator events that trigger the an alog comparator interrupt. the different settings are shown in table 13-2 on page 81 . when changing the acis1/acis0 bits, the analog comparator interrupt must be disabled by clearing its interrupt enable bit in the acsr re gister. otherwise an interrupt can occur when the bits are changed. 13.2.3 didr0 ? digital i nput disable register 0 ? bits 1, 0 ? ain1d, ain0d: ai n1, ain0 digital input disable when this bit is written logic one, the digital inpu t buffer on the ain1/0 pin is disabled. the corre- sponding pin register bit will alwa ys read as zero when this bit is set. when an analog signal is applied to the ain1/0 pin and the digital input from this pin is not needed, this bit should be writ- ten logic one to reduce power consumption in the digital input buffer. table 13-2. acis1/acis0 settings acis1 acis0 interrupt mode 0 0 comparator interrupt on output toggle. 01reserved 1 0 comparator interrupt on falling output edge. 1 1 comparator interrupt on rising output edge. bit 76543210 0x14 ? ? adc0d adc2d adc3d adc1d ain1d ain0d didr0 read/write r r r/w r/w r/w r/w r/w r/w initial value00000000
82 8126d?avr?11/09 attiny13a 14. analog to digital converter 14.1 features ? 10-bit resolution ? 0.5 lsb integral non-linearity ? 2 lsb absolute accuracy ? 13 - 260 s conversion time ? up to 15 ksps at maximum resolution ? four multiplexed single ended input channels ? optional left adjustment for adc result readout ? 0 - v cc adc input voltage range ? selectable 1.1v adc reference voltage ? free running or single conversion mode ? adc start conversion by auto tr iggering on interrupt sources ? interrupt on adc conversion complete ? sleep mode noise canceler 14.2 overview the attiny13a features a 10-bit successive approximation adc. a block diagram of the adc is shown in figure 14-1 . figure 14-1. analog to digital converter block schematic adc conversion complete irq 8-bit data bus 15 0 adc multiplexer select (admux) adc ctrl. & status register (adcsra) adc data register (adch/adcl) adie adate adsc aden adif adif mux1 mux0 adps0 adps1 adps2 conversion logic 10-bit dac + - sample & hold comparator internal 1.1v reference mux decoder v cc adc3 adc2 adc1 adc0 refs1 adlar channel selection adc[9:0] adc multiplexer output prescaler input mux trigger select adts[2:0] interrupt flags start
83 8126d?avr?11/09 attiny13a the adc is connected to a 4-channel analog mult iplexer which allows fo ur single-ended voltage inputs constructed from the pins of port b. the single-ended voltage inputs refer to 0v (gnd). the adc contains a sample and hold circuit whic h ensures that the input voltage to the adc is held at a constant level during conversion. inte rnal reference voltages of nominally 1.1v or v cc are provided on-chip. 14.3 operation the adc converts an analog input voltage to a 10-bit digital value through successive approxi- mation. the minimum value represents gnd and the maximum value represents the voltage on v cc or an internal 1.1v reference voltage. the analog input channel is selected by writing to the mux bits in admu x. any of the adc input pins, can be selected as single ended inputs to the adc. the adc is enabled by setting the adc enable bit, aden in adcsra. voltage reference and input channel selections will not go into effect until aden is set. the adc does not consume power when aden is cleared, so it is recommende d to switch off the adc before entering power saving sleep modes. the adc generates a 10-bit result which is pr esented in the adc data registers, adch and adcl. by default, the result is presented right adjusted, but can optionally be presented left adjusted by setting the adlar bit in admux. if the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read adch. otherwise, adcl must be r ead first, then adch, to ensure that the content of the data registers belongs to the same conversion. once adcl is read, adc access to data registers is blocked. this means that if adcl has been r ead, and a conversion completes before adch is read, neither register is updated and the result fr om the conversion is lost. when adch is read, adc access to the adch and ad cl registers is re-enabled. the adc has its own interrupt which can be tri ggered when a conversion completes. when adc access to the data registers is prohibited between r eading of adch and ad cl, the in terrupt will trigger even if the result is lost. 14.4 starting a conversion a single conversion is started by writing a l ogical one to the adc start conversion bit, adsc. this bit stays high as long as the conversi on is in progress and will be cleared by hardware when the conversion is completed. if a different data channel is selected while a conversion is in progress, the adc will finish the current conv ersion before performing the channel change. alternatively, a conversion can be triggered auto matically by various sour ces. auto triggering is enabled by setting the adc auto trigger enable bi t, adate in adcsra. the trigger source is selected by setting the adc trigger select bits, adts in adcsrb (see description of the adts bits for a list of the trigger sources). when a positive edge occurs on the selected trigger signal, the adc prescaler is reset and a conversion is st arted. this provides a method of starting con- versions at fixed intervals. if the trigger signal still is set when the conversion completes, a new conversion will not be started. if another positive edge occurs on the trigger signal during con- version, the edge will be ignored. note that an interrupt flag will be set even if the specific interrupt is disabled or the global interrupt enab le bit in sreg is cleared. a conversion can thus be triggered without causing an interrupt. however, the interrupt flag must be cleared in order to trigger a new conversion at the next interrupt event.
84 8126d?avr?11/09 attiny13a figure 14-2. adc auto trigger logic using the adc interrupt flag as a trigger sour ce makes the adc start a new conversion as soon as the ongoing conversion has finished. the adc then operates in free running mode, con- stantly sampling and updating the adc data register. the first conversion must be started by writing a logical one to the adsc bit in adcs ra. in this mode the adc will perform successive conversions independently of whether the a dc interrupt flag, adif is cleared or not. if auto triggering is enabled, single conversi ons can be started by writing adsc in adcsra to one. adsc can also be used to determine if a conversion is in progress. the adsc bit will be read as one during a conversion, independe ntly of how the conversion was started. 14.5 prescaling and conversion timing by default, the successive approximation circuitry requires an input clock frequency between 50 khz and 200 khz to get maximum resolution. if a lower resolution than 10 bits is needed, the input clock frequency to the adc can be higher than 200 khz to get a higher sample rate. figure 14-3. adc prescaler the adc module contains a prescaler, which generates an acceptab le adc clock frequency from any cpu frequency above 100 khz. the presca ling is set by the adps bits in adcsra. adsc adif source 1 source n adts[2:0] conversion logic prescaler start clk adc . . . . edge detector adate 7-bit adc prescaler adc clock source ck adps0 adps1 adps2 ck/128 ck/2 ck/4 ck/8 ck/16 ck/32 ck/64 reset aden start
85 8126d?avr?11/09 attiny13a the prescaler starts counting from the moment th e adc is switched on by setting the aden bit in adcsra. the prescaler keeps running for as long as the aden bit is set, and is continuously reset when aden is low. when initiating a single ended conversion by se tting the adsc bit in adcsra, the conversion starts at the following rising edge of the adc clock cycle. a normal conversion takes 13 adc clock cycles. the first conversion after the adc is switched on (aden in adcsra is set) takes 25 adc clock cycles in order to initialize the analog circuitry, as shown in figure 14-4 below. figure 14-4. adc timing diagram, first conver sion (single conversion mode) when the bandgap reference voltage is used as input to the adc, it will take a certain time for the voltage to stabilize. if not stabilized, the fi rst value read after the first conversion may be wrong. the actual sample-and-hold takes place 1.5 adc cl ock cycles after the start of a normal conver- sion and 14.5 adc clock cycles after the start of an first conversion. when a conversion is complete, the result is written to the adc data re gisters, and adif is set. in single conversion mode, adsc is cleared simultaneously. the software may then set adsc again, and a new conversion will be init iated on the first rising adc clock edge. figure 14-5. adc timing diagram, single conversion sign and msb of result lsb of result adc clock adsc sample & hold adif adch adcl cycle number aden 1 212 13 14 15 16 17 18 19 20 21 22 23 24 25 1 2 first conversion next conversion 3 mux and refs update mux and refs update conversion complete 1 2 3 4 5 6 7 8 9 10 11 12 13 sign and msb of result lsb of result adc clock adsc adif adch adcl cycle number 12 one conversion next conversion 3 sample & hold mux and refs update conversion complete mux and refs update
86 8126d?avr?11/09 attiny13a when auto triggering is used, the prescaler is reset when the trigger event occurs, as shown in figure 14-6 below. this assures a fixed delay from the trigger event to the st art of conversion. in this mode, the sample-and-hold takes place two adc clock cycles after the rising edge on the trigger source signal. three additional cpu cl ock cycles are used for synchronization logic. figure 14-6. adc timing diagram, auto triggered conversion in free running mode, a new conversion will be started immediately af ter the conversion com- pletes, while adsc remains high. figure 14-7. adc timing diagram, free running conversion 1 2 3 4 5 6 7 8 9 10 11 12 13 sign and msb of result lsb of result adc clock trigger source adif adch adcl cycle number 12 one conversion next conversion conversion complete prescaler reset adate prescaler reset sample & hold mux and refs update 11 12 13 sign and msb of result lsb of result adc clock adsc adif adch adcl cycle number 12 one conversion next conversion 34 conversion complete sample & hold mux and refs update
87 8126d?avr?11/09 attiny13a for a summary of conversion times, see table 14-1 . 14.6 changing channel or reference selection the muxn and refs1:0 bits in the admux regi ster are single buffered through a temporary register to which the cpu has random access. this ensures that the channels and reference selection only takes place at a safe point dur ing the conversion. the channel and reference selection is continuously updated until a conversion is started. once the conversion starts, the channel and reference selection is locked to ensu re a sufficient sampling time for the adc. con- tinuous updating resumes in the last adc cloc k cycle before the conversion completes (adif in adcsra is set). note that the conversion star ts on the following rising adc clock edge after adsc is written. the user is thus advised not to write new channel or reference selection values to admux until one adc clock cycle after adsc is written. if auto triggering is used, the exact time of t he triggering event can be indeterministic. special care must be taken when updating the admux register, in order to control which conversion will be affected by the new settings. if both adate and aden is writ ten to one, an interrupt event can occur at any time. if the admux register is changed in this period, the user cannot tell if the next conversion is based on the old or the new settings. admux can be safely updated in the following ways: ? when adate or aden is cleared. ? during conversion, minimum one adc clock cycle after the trigger event. ? after a conversion, before the interrupt flag used as trigger source is cleared. when updating admux in one of these conditions, the new settings will affect the next adc conversion. 14.6.1 adc input channels when changing channel selections, the user should observe the following guidelines to ensure that the correct channel is selected: in single conversion mode, always select the channel before starting the conversion. the chan- nel selection may be changed one adc clock cycle after writing one to adsc. however, the simplest method is to wait for the conversion to complete before changing the channel selection. in free running mode, always select the channel before starting the first conversion. the chan- nel selection may be changed one adc clock cycle after writing one to adsc. however, the simplest method is to wait for the first conversion to complete, and then change the channel selection. since the next conver sion has already started automati cally, the next result will reflect the previous channel selection. subsequent conversions will refl ect the new channel selection. table 14-1. adc conversion time condition sample & hold (cycles from start of conversion) conversion time (cycles) first conversion 13.5 25 normal conversions 1.5 13 auto triggered conversions 2 13.5
88 8126d?avr?11/09 attiny13a 14.6.2 adc voltage reference the reference voltage for the adc (v ref ) indicates the conversion range for the adc. single ended channels that exceed v ref will result in code s close to 0x3ff. v ref can be selected as either v cc , or internal 1.1v reference. the first ad c conversion result afte r switching reference voltage source may be inaccurate, and the user is advised to discard this result. 14.7 adc noise canceler the adc features a noise canceler that enables conversion during sleep mode to reduce noise induced from the cpu core and other i/o peripher als. the noise canceler can be used with adc noise reduction and idle mode. to make use of this feature, the following procedure should be used: ? make sure that the adc is enabled and is not busy converting. single conversion mode must be selected and the adc conversion complete interrupt must be enabled. ? enter adc noise reduction mode (or idle mode ). the adc will start a conversion once the cpu has been halted. ? if no other interrupts occur before the adc conversion completes, the adc interrupt will wake up the cpu and execute the adc conversi on complete interrupt routine. if another interrupt wakes up the cpu before the adc co nversion is complete, the interrupt will be executed, and an adc conversi on complete interrup t request will be generated when the adc conversion completes. the cpu will remain in active mode un til a new sleep command is executed. note that the adc will no t be automatically turned off when entering other sleep modes than idle mode and adc noise reduction mode. the user is advised to write zero to aden before enter- ing such sleep modes to avoid excessive power consumption. 14.8 analog input circuitry the analog input circuitry for single ended channels is shown in figure 14-8 an analog source applied to adcn is subjected to pin capacitance and input leakage of that pin, regardless if the channel is chosen as input for the adc, or not. when the channel is selected, the source drives the s/h capacitor through the series resist ance (combined resistance in input path). figure 14-8. analog input circuitry note: the capacitor in the figure depicts the total capacitance, including the sample/hold capacitor and any stray or parasitic capacitance inside t he device. the value given is worst case. n i ih 1..100 k ohm c s/h = 14 pf i il
89 8126d?avr?11/09 attiny13a the adc is optimized for analog signals wit h an output impedance of approximately 10 k or less. if such a source is used, the sampling time will be negligible. if a source with higher imped- ance is used, the sampling time will depend on how long time the source nee ds to charge the s/h capacitor, with can vary widely. the user is recommended to only use low impedant sources with slowly varying signals, since this minimizes the required charge transfer to the s/h capacitor. signal components higher than the nyquist frequency (f adc /2) should not be present to avoid distortion from unpredictable signal convolution. the user is advised to remove high frequency components with a low-pass filter before appl ying the signals as inputs to the adc. 14.9 analog noise ca nceling techniques digital circuitry inside and outside the device ge nerates emi which might affect the accuracy of analog measurements. when conversion accuracy is critical, the noise level can be reduced by applying the following techniques: ? keep analog signal paths as short as possible. ? make sure analog tracks run over the analog ground plane. ? keep analog tracks well away from high-speed switching digital tracks. ? if any port pin is used as a digital output, it mustn?t switch while a conversion is in progress. ? place bypass capacitors as close to v cc and gnd pins as possible. where high adc accuracy is required it is recommended to use adc noise reduction mode, as described in section 14.7 on page 88 . this is especially the case when system clock frequency is above 1 mhz. a good system design with pro perly placed, external bypass capacitors does reduce the need for using adc noise reduction mode 14.10 adc accuracy definitions an n-bit single-ended adc converts a voltage linearly between gnd and v ref in 2 n steps (lsbs). the lowest code is read as 0, and the highest code is read as 2 n -1. several parameters describe the deviation from the ideal behavior:
90 8126d?avr?11/09 attiny13a ? offset: the deviation of the first transition (0 x000 to 0x001) compared to the ideal transition (at 0.5 lsb). ideal value: 0 lsb. figure 14-9. offset error ? gain error: after adjusting for offset, the gain error is found as the deviation of the last transition (0x3fe to 0x3ff) compared to the ideal transition (at 1.5 lsb below maximum). ideal value: 0 lsb figure 14-10. gain error o u tp u t code v ref inp u t volt a ge ide a l adc act ua l adc off s et error o u tp u t code v ref inp u t volt a ge ide a l adc act ua l adc g a in error
91 8126d?avr?11/09 attiny13a ? integral non-linearity (inl): afte r adjusting for offset and gain error, the inl is the maximum deviation of an actual transition compared to an ideal transition for any code. ideal value: 0 lsb. figure 14-11. integral non-linearity (inl) ? differential non-linearity (dnl): the maximum deviation of the actu al code width (the interval between two adjacent transitions) from the ideal code width (1 lsb). ideal value: 0 lsb. figure 14-12. differential non-linearity (dnl) o u tp u t code v ref inp u t volt a ge ide a l adc act ua l adc inl o u tp u t code 0x 3 ff 0x000 0 v ref inp u t volt a ge dnl 1 l s b
92 8126d?avr?11/09 attiny13a ? quantization error: due to the quantization of th e input voltage into a finite number of codes, a range of input volt ages (1 lsb wide) will code to the same value. always 0.5 lsb. ? absolute accuracy: the maximum deviation of an actual (unadjusted) transition compared to an ideal transition for any code. th is is the compound effect of offset, gain error, differential error, non-linearity, and quantization error. ideal value: 0.5 lsb. 14.11 adc conversion result after the conversion is complete (adif is high ), the conversion result can be found in the adc result registers (adcl, adch). for single ended conversion, the result is where v in is the voltage on the se lected input pin and v ref the selected voltage reference (see table 14-2 on page 92 and table 14-3 on page 93 ). 0x000 represents analog ground, and 0x3ff represents the selected reference voltage minus one lsb. 14.12 register description 14.12.1 admux ? adc multiplexer selection register ? bits 7, 4:2 ? res: reserved bits these bits are reserved bits in the attiny13a and will always read as zero. ? bit 6 ? refs0: reference selection bit this bit selects the voltage reference for the adc, as shown in table 14-2 . if this bit is changed during a conversion, the change will not go in effe ct until this conversion is complete (adif in adcsra is set). ? bit 5 ? adlar: adc left adjust result the adlar bit affects the presentation of the adc conversion result in the adc data register. write one to adlar to left adjust the result. otherw ise, the result is right adjusted. changing the adlar bit will affect t he adc data register immediately, regardless of any ongoing conver- sions. for a complete description of this bit, see ?adcl and adch ? the adc data register? on page 94 . adc v in 1024 ? v ref -------------------------- = bit 76543210 0x07 ? refs0 adlar ? ? ? mux1 mux0 admux read/write r r/w r/w r r r r/w r/w initial value00000000 table 14-2. voltage reference selections for adc refs0 voltage reference selection 0v cc used as analog reference. 1 internal voltage reference.
93 8126d?avr?11/09 attiny13a ? bits 1:0 ? mux1:0: analog channel selection bits the value of these bits selects which combinatio n of analog inputs are connected to the adc. see table 14-3 for details. if these bits are changed during a conversion, the change will not go in effect until this conversion is complete (adif in adcsra is set). 14.12.2 adcsra ? adc control and status register a ? bit 7 ? aden: adc enable writing this bit to one enables the adc. by writi ng it to zero, the adc is turned off. turning the adc off while a conversion is in prog ress, will terminate this conversion. ? bit 6 ? adsc: adc start conversion in single conversion mode, write this bit to one to start each conversion. in free running mode, write this bit to one to start the first conversion. the first conversion after adsc has been written after the adc has been enabled, or if adsc is written at the same time as the adc is enabled, will take 25 adc clock cycles instead of the norma l 13. this first conversi on performs initializa- tion of the adc. adsc will read as one as long as a conversion is in progress. when the co nversion is complete, it returns to zero. writing zero to this bit has no effect. ? bit 5 ? adate: adc auto trigger enable when this bit is written to on e, auto triggering of the adc is enabled. the adc will start a con- version on a positive edge of the selected trigger signal. the trigger source is selected by setting the adc trigger select bits, adts in adcsrb. ? bit 4 ? adif: adc interrupt flag this bit is set when an adc conversion complete s and the data registers are updated. the adc conversion complete interrupt is executed if th e adie bit and the i-bit in sreg are set. adif is cleared by hardware when executing the corres ponding interrupt handling vector. alternatively, adif is cleared by writing a logical one to the flag. beware that if doing a read-modify-write on adcsra, a pending interrupt can be disabled. this also applies if the sbi and cbi instructions are used. table 14-3. input channel selections mux1..0 single ended input 00 adc0 (pb5) 01 adc1 (pb2) 10 adc2 (pb4) 11 adc3 (pb3) bit 76543210 0x06 aden adsc adate adif adie adps2 adps1 adps0 adcsra read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
94 8126d?avr?11/09 attiny13a ? bit 3 ? adie: adc interrupt enable when this bit is written to one and the i-bit in sreg is set, the adc conversion complete inter- rupt is activated. ? bits 2:0 ? adps2:0: adc prescaler select bits these bits determine the division factor betwee n the system clock frequency and the input clock to the adc. 14.12.3 adcl and adch ? the adc data register 14.12.3.1 adlar = 0 14.12.3.2 adlar = 1 when an adc conversion is complete, the result is found in these two registers. when adcl is read, the adc data register is not updated unt il adch is read. consequently, if the result is left adjusted and no more than 8-bit precision is requ ired, it is sufficient to read adch. otherwise, adcl must be read first, then adch. table 14-4. adc prescaler selections adps2 adps1 adps0 division factor 000 2 001 2 010 4 011 8 100 16 101 32 110 64 111 128 bit 151413121110 9 8 0x05 ?????? adc9 adc8 adch 0x04 adc7 adc6 adc5 adc4 adc3 adc2 adc1 adc0 adcl 76543210 read/writerrrrrrrr rrrrrrrr initial value00000000 00000000 bit 151413121110 9 8 0x05 adc9 adc8 adc7 adc6 adc5 adc4 adc3 adc2 adch 0x04 adc1 adc0 ??????adcl 76543210 read/writerrrrrrrr rrrrrrrr initial value00000000 00000000
95 8126d?avr?11/09 attiny13a the adlar bit in admux, and the muxn bits in admux affect the way the result is read from the registers. if adlar is set, the result is left adjusted. if adla r is cleared (default), the result is right adjusted. ? adc9:0: adc conversion result these bits represent the result fr om the conversion, as detailed in ?adc conversion result? on page 92 . 14.12.4 adcsrb ? adc control and status register b ? bits 7, 5:3 ? res: reserved bits these bits are reserved bits in the attiny13a and will always read as zero. ? bits 2:0 ? adts2:0: adc auto trigger source if adate in adcsra is written to one, the value of these bits selects which source will trigger an adc conversion. if adate is cleared, the adts2:0 settings will have no effect. a conversion will be triggered by the risi ng edge of the selected interrupt flag . note that switch ing from a trig- ger source that is cleared to a trigger source that is set, will generate a positive edge on the trigger signal. if aden in adcsra is set, this will start a conversion. switching to free running mode (adts[2:0]=0) will not cause a trigger event, even if t he adc interrupt flag is set . 14.12.5 didr0 ? digital input disable register 0 ? bits 5:2 ? adc3d:adc0d: adc3:0 digital input disable when a bit is written logic one, the digital input buffer on the corresponding adc pin is disabled. the corresponding pin register bi t will always read as zero when th is bit is set. when an analog signal is applied to the adc7..0 pin and the digital input from this pin is not needed, this bit should be written logic one to reduce power consumption in the digital input buffer. bit 76543210 0x03 ? acme ? ? ? adts2 adts1 adts0 adcsrb read/write r r/w r r r r/w r/w r/w initial value00000000 table 14-5. adc auto trigger source selections adts2 adts1 adts0 trigger source 0 0 0 free running mode 0 0 1 analog comparator 0 1 0 external interrupt request 0 0 1 1 timer/counter compare match a 1 0 0 timer/counter overflow 1 0 1 timer/counter compare match b 1 1 0 pin change interrupt request bit 76543210 0x14 ? ? adc0d adc2d adc3d adc1d ain1d ain0d didr0 read/write r r r/w r/w r/w r/w r/w r/w initial value00000000
96 8126d?avr?11/09 attiny13a 15. debugwire on-ch ip debug system 15.1 features ? complete program flow control ? emulates all on-chip functions, both digital and analog, except reset pin ? real-time operation ? symbolic debugging support (both at c and assembler source level, or for other hlls) ? unlimited number of program break points (using software break points) ? non-intrusive operation ? electrical characte ristics identical to real device ? automatic configuration system ? high-speed operation ? programming of non-volatile memories 15.2 overview the debugwire on-chip debug system uses a one-wire, bi-directional interface to control the program flow, execute avr instructions in t he cpu and to program the different non-volatile memories. 15.3 physical interface when the debugwire enable (dwen) fuse is programmed and lock bits are unprogrammed, the debugwire system within the target device is activated. the reset port pin is configured as a wire-and (open-drain) bi-directional i/o pin with pull-up enabled and becomes the commu- nication gateway between target and emulator. figure 15-1 shows the schematic of a target mcu, with debugwire enabled, and the emulator connector. the system clock is not affected by debugwire and will always be the clock source selected by the cksel fuses. figure 15-1. the debugwire setup d w gnd dw(reset) vcc 1.8 - 5.5 v
97 8126d?avr?11/09 attiny13a when designing a system where debugwire will be used, the following must be observed: ? pull-up resistor on the dw/(reset) line mu st be in the range of 10k to 20 k . however, the pull-up resistor is optional. ? connecting the reset pin directly to v cc will not work. ? capacitors inserted on the reset pin must be disconnected w hen using debugwire. ? all external reset sources must be disconnected. 15.4 software break points debugwire supports program memory break points by the avr break instruction. setting a break point in avr studio ? will insert a break instruction in the program memo ry. the instruc- tion replaced by the break instru ction will be stored. when program execution is continued, the stored instruction will be execut ed before continuing from the program memory. a break can be inserted manually by putting the break instruction in the program. the flash must be re-programmed each time a break point is changed. this is automatically handled by avr studio th rough the debugwire inte rface. the use of brea k points will therefore reduce the flash data retention. devices used for debugging purposes should not be shipped to end customers. 15.5 limitations of debugwire the debugwire communication pin (dw) is physica lly located on the same pin as external reset (reset). an external re set source is therefore not supported when the debugwire is enabled. the debugwire system accurately emulates all i/ o functions when running at full speed, i.e., when the program in the cpu is running. when the cpu is stopped, care must be taken while accessing some of the i/o registers via the de bugger (avr studio). see the debugwire docu- mentation for detailed descri ption of the limitations. the debugwire interface is asynchronous, whic h means that the debugger needs to synchro- nize to the system clock. if the system clock is changed by software (e.g . by writing clkps bits) communication via debugwire may fail. also, clock frequencies below 100 khz may cause communication problems. a programmed dwen fuse enables some parts of the clock system to be running in all sleep modes. this will increase the po wer consumption while in sleep. thus, the dwen fuse should be disabled when debugwire is not used. 15.6 register description the following section describes the registers used with the debugwire. 15.6.1 dwdr ?debugwire data register the dwdr register provides a communication channel from the running program in the mcu to the debugger. this register is only accessible by the debugwire and can therefore not be used as a general purpose register in the normal operations. bit 76543210 0x2e dwdr[7:0] dwdr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
98 8126d?avr?11/09 attiny13a 16. self-programming the flash the device provides a self-programming me chanism for downloading and uploading program code by the mcu itself. the self-programming ca n use any available data interface and associ- ated protocol to read code and write (progra m) that code into the program memory. the spm instruction is disabled by default but it ca n be enabled by programming the selfprgen fuse (to ?0?). the program memory is updated in a page by page fashion. before programming a page with the data stored in the temporary page buffer, the page must be erased. the temporary page buf- fer is filled one word at a time using spm and the buffer can be filled either before the page erase command or between a page erase and a page write operation: alternative 1, fill the bu ffer before a page erase ? fill temporary page buffer ? perform a page erase ? perform a page write alternative 2, fill the bu ffer after page erase ? perform a page erase ? fill temporary page buffer ? perform a page write if only a part of the page needs to be changed, th e rest of the page must be stored (for example in the temporary page buffer) before the erase, and then be re-written. when using alternative 1, the boot loader provides an effective read-mod ify-write feature which a llows the user software to first read the page, do the necessary changes, and then write back the modified data. if alter- native 2 is used, it is not possible to read the old data while loading since the page is already erased. the temporary page buffer can be accessed in a random sequence. it is essential that the page address used in both the page erase and page write operation is addressing the same page. 16.1 performing page erase by spm to execute page erase, set up the address in the z-pointer, write ?00000011? to spmcsr and execute spm within four clock cycl es after writing spmcsr. the data in r1 and r0 is ignored. the page address must be written to pcpage in the z-register. other bits in the z-pointer will be ignored during this operation. note: the cpu is halted duri ng the page erase operation. 16.2 filling the temporary buffer (page loading) to write an instruction word, set up the addres s in the z-pointer and data in r1:r0, write ?00000001? to spmcsr and execute spm within four clock cycles after writing spmcsr. the content of pcword in the z-register is used to address the data in the temporary buffer. the temporary buffer will auto-erase after a page write operation or by writing the ctpb bit in spmcsr. it is also erased after a system reset. no te that it is not possible to write more than one time to each address without erasing the temporary buffer.
99 8126d?avr?11/09 attiny13a if the eeprom is wr itten in the middle of an spm page load operation, all data loaded will be lost. 16.3 performing a page write to execute page write, set up the address in the z-pointer, write ?00000101? to spmcsr and execute spm within four clock cycl es after writing spmcsr. the data in r1 and r0 is ignored. the page address must be written to pcpage. ot her bits in the z-point er must be written to zero during this operation. note: the cpu is halted during the page write operation. 16.4 addressing the flash during self-programming the z-pointer is used to address the spm commands. since the flash is organized in pages (see table 17-5 on page 105 ), the program counter can be treated as having two different sections. one sect ion, consisting of the least significant bits, is addressing the words within a page, while the most significant bits are addressing the pages. this is shown in figure 16-1 . note that the page erase and page write operations are addressed independently. therefore it is of major importance that the software addresses the same page in both the page erase and page write operation. figure 16-1. addressing the flash during spm (1) note: 1. the variables used in figure 16-1 are listed in table 17-5 on page 105 . bit 151413121110 9 8 zh (r31) z15 z14 z13 z12 z11 z10 z9 z8 zl (r30) z7z6z5z4z3z2z1z0 76543210 program memory 0 1 15 z - register bit 0 zpagemsb word address within a page page address within the flash zpcmsb instruction word pag e pcword[pagemsb:0]: 00 01 02 pageend pag e pcword pcpage pcmsb pagemsb program counter
100 8126d?avr?11/09 attiny13a the lpm instruction uses the z-pointer to store the address. since this instruction addresses the flash byte-by-byte, also the lsb (bit z0) of the z-pointer is used. 16.5 eeprom write prevents writing to spmcsr note that an eeprom write oper ation will block all software progra mming to flash. reading the fuses and lock bits from software will also be pr evented during the eeprom write operation. it is recommended that the user checks the status bi t (eepe) in the eecr regi ster and ve rifies that the bit is cleared before writing to the spmcsr register. 16.6 reading fuse and lo ck bits from firmware it is possible to read fuse and lock bits from software. 16.6.1 reading lock bits from firmware issuing an lpm instruction within three cpu cycles after rflb and selfprgen bits have been set in spmcsr will return lock bit values in the destination register. the rflb and self- prgen bits automatically clear upon completion of reading the lock bits, or if no lpm instruction is executed within three cpu cycles, or if no spm instruction is executed within four cpu cycles. when rflb and selfprgen are cleared, lpm functions normally. to read the lock bits, follow the below procedure. 1. load the z-pointer with 0x0001. 2. set rflb and selfprgen bits in spmcsr. 3. issuing an lpm instruction within three clo ck cycles will return lock bits in the destina- tion register. if successful, the contents of the destination register are as follows. see section ?program and data memory lock bits? on page 103 for more information on lock bits. 16.6.2 reading fuse bits from firmware the algorithm for reading fuse byte s is similar to the one described above for reading lock bits, only the addresses are different. to read the fuse low byte (flb), follow the below procedure: 1. load the z-pointer with 0x0000. 2. set rflb and selfprgen bits in spmcsr. 3. issuing an lpm instruction within three clo ck cycles will flb in the destination register. if successful, the contents of the destination register are as follows. bit 76543210 rd ??????lb2lb1 bit 76543210 rd flb7 flb6 flb5 flb4 flb3 flb2 flb1 flb0
101 8126d?avr?11/09 attiny13a to read the fuse high byte (fhb), simply repl ace the address in the z-pointer with 0x0003 and repeat the procedure above. if successful, the contents of the destination register are as follows. see sections ?program and data memory lock bits? on page 103 and ?fuse bytes? on page 104 for more information on fuse and lock bits. 16.7 preventing flash corruption during periods of low v cc , the flash program can be corrupted because the supply voltage is too low for the cpu and the flash to operate properly. these issues are the same as for board level systems using the flash, and the same design solutions should be applied. a flash program corruption can be caused by two situ ations when the voltage is too low. first, a regular write sequence to the flash requires a minimum voltage to operate correctly. secondly, the cpu itself can execute instruct ions incorrectly, if the supply voltage for executing instructions is too low. flash corruption can easily be avoided by following these design recommendations (one is sufficient): 1. keep the avr reset active (low) during peri ods of insufficient po wer supply voltage. this can be done by enabling the internal brown-out detector (bod) if the operating voltage matches the detection level. if not, an external low v cc reset protection circuit can be used. if a reset occurs while a write operation is in progress, the write operation will be completed provided that the po wer supply voltage is sufficient. 2. keep the avr core in power-down sleep mode during periods of low v cc . this will pre- vent the cpu from attempting to decode and execute instructions, effectively protecting the spmcsr register and thus the flash from unintentional writes. 16.8 programming time for flash when using spm the calibrated rc oscillator is used to time flash accesses. table 16-1 on page 101 shows the typical programming time for flash accesses from the cpu. note: 1. the min and max programming times is per individual operation. bit 76543210 rd fhb7 fhb6 fhb5 fhb4 fhb3 fhb2 fhb1 fhb0 table 16-1. spm programming time (1) symbol min programming time max programming time flash write (page erase, page write, and write lock bits by spm) 3.7 ms 4.5 ms
102 8126d?avr?11/09 attiny13a 16.9 register description 16.9.1 spmcsr ? store program memory control and status register the store program memory control and status regi ster contains the control bits needed to con- trol the program memory operations. ? bits 7:5 ? res: reserved bits these bits are reserved bits in the attiny13a and always read as zero. ? bit 4 ? ctpb: clear temporary page buffer if the ctpb bit is writte n while filling the temporary page bu ffer, the temporary page buffer will be cleared and the da ta will be lost. ? bit 3 ? rflb: read fuse and lock bits an lpm instruction within th ree cycles after rflb and se lfprgen are set in the spmcsr register, will read either the lock bits or the fuse bits (depending on z0 in the z-pointer) into the destination register. see ?eeprom write prevents writi ng to spmcsr? on page 100 for details. ? bit 2 ? pgwrt: page write if this bit is written to one at the same time as selfprgen, the next sp m instruction within four clock cycles executes page write, with the data stored in the temporary buffer. the page address is taken from the high part of the z-pointer. the data in r1 and r0 are ignored. the pgwrt bit will auto-clear upon completion of a page write, or if no spm instruction is executed within four clock cycles. the cpu is halte d during the entire page write operation. ? bit 1 ? pgers: page erase if this bit is written to one at the same time as selfprgen, the next sp m instruction within four clock cycles executes page erase. the page address is taken from the high part of the z- pointer. the data in r1 and r0 are ignored. t he pgers bit will auto-clear upon completion of a page erase, or if no spm instruction is executed within four clock cycles. the cpu is halted dur- ing the entire page write operation. ? bit 0 ? selfprgen: self programming enable this bit enables the spm instruction for the next fo ur clock cycles. if written to one together with either ctpb, rflb, pgwrt, or pgers, the following spm instruction will have a special meaning, see description above. if only selfprgen is written, the follow ing spm instruction will store the value in r1:r0 in the temporary pa ge buffer addressed by the z-pointer. the lsb of the z-pointer is ignored. the selfprgen bit will auto-clear upon completion of an spm instruction, or if no spm instruction is execut ed within four clock cycles. during page erase and page write, the selfprgen bit remains high until the operation is completed. writing any other combination than ?10001?, ?01001?, ?00101?, ?00011? or ?00001? in the lower five bits will have no effect. bit 7654321 0 0x37 ? ? ? ctpb rflb pgwrt pgers selfprgen spmcsr read/write r r r r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
103 8126d?avr?11/09 attiny13a 17. memory programming this section describes how attiny 13a memories can be programmed. 17.1 program and data memory lock bits attiny13a provides two lock bits which can be left unprogrammed (?1?) or can be programmed (?0?) to obtain the additional security listed in table 17-2 on page 103 . the lock bits can be erased to ?1? with the chip erase command, only. program memory can be read out via the debug wire interface when the dwen fuse is pro- grammed, even if the lock bits are set. thus, when lock bit security is required, debugwire should always be disabled by clearing the dwen fuse. note: 1. ?1? means unprogrammed, ?0? means programmed notes: 1. program fuse bits before lock bits. see section ?fuse bytes? on page 104 . 2. ?1? means unprogrammed, ?0? means programmed table 17-1. lock bit byte lock bit byte bit no desc ription default value (1) 7 ? 1 (unprogrammed) 6 ? 1 (unprogrammed) 5 ? 1 (unprogrammed) 4 ? 1 (unprogrammed) 3 ? 1 (unprogrammed) 2 ? 1 (unprogrammed) lb2 1 lock bit 1 (unprogrammed) lb1 0 lock bit 1 (unprogrammed) table 17-2. lock bit protection modes memory lock bits (1) (2) protection type lb mode lb2 lb1 1 1 1 no memory lock features enabled. 210 further programming of the flas h and eeprom is disabled in high-voltage and serial programming mode. fuse bits are locked in both serial and high-voltage programming mode. debugwire is disabled. 300 further programming and verification of the flash and eeprom is disabled in high-voltage and serial programming mode. fuse bits are locked in both serial and high-voltage programming mode. debugwire is disabled.
104 8126d?avr?11/09 attiny13a 17.2 fuse bytes the attiny13a has two fuse bytes. table 17-3 on page 104 and table 17-4 on page 104 describe briefly the functionality of all the fuses and how they are mapped into the fuse bytes. note that the fuses are read as logical zero, ?0?, if they are programmed. notes: 1. enables spm instruction. see ?self-programming the flash? on page 98 . 2. dwen must be unprogrammed when lock bit security is required. see ?program and data memory lock bits? on page 103. 3. see table 18-6 on page 120 for bodlevel fuse decoding. 4. see ?alternate functions of port b? on page 55 for description of rstd isbl and dwen fuses. when programming the rstdisbl fuse, high-voltage serial programming has to be used to change fuses to perform further programming. notes: 1. the spien fuse is not accessible in spi programming mode. 2. programming this fues will disable the watchdog timer interrupt. see ?watchdog timer? on page 38 for details. 3. see ?system clock prescaler? on page 26 for details. 4. the default value of sut1..0 results in maximum start-up time for the default clock source. see table 18-3 on page 119 for details. 5. the default setting of cksel1..0 results in internal rc oscillator @ 9.6 mhz. see table 18-3 on page 119 for details. table 17-3. fuse high byte fuse bit bit no description default value ? 7 ? 1 (unprogrammed) ? 6 ? 1 (unprogrammed) ? 5 ? 1 (unprogrammed) selfprgen (1) 4 self programming enable 1 (unprogrammed) dwen (2) 3 debugwire enable 1 (unprogrammed) bodlevel1 (3) 2 brown-out detector trigger level 1 (unprogrammed) bodlevel0 (3) 1 brown-out detector trigger level 1 (unprogrammed) rstdisbl (4) 0 external reset disable 1 (unprogrammed) table 17-4. fuse low byte fuse bit bit no description default value spien (1) 7 enable serial programming and data downloading 0 (programmed) (spi prog. enabled) eesave 6 preserve eeprom memory through chip erase 1 (unprogrammed) (memory not preserved) wdton (2) 5 watchdog timer always on 1 (unprogrammed) ckdiv8 (3) 4 divide clock by 8 0 (programmed) sut1 (4) 3 select start-up time 1 (unprogrammed) sut0 (4) 2 select start-up time 0 (programmed) cksel1 (5) 1 select clock source 1 (unprogrammed) cksel0 (5) 0 select clock source 0 (programmed)
105 8126d?avr?11/09 attiny13a note that fuse bits are locked if lock bit 1 (lb1) is programmed. program the fuse bits before programming the lock bits. the status of t he fuse bits is not affected by chip erase. fuse bits can also be read by the device firmware. see section ?reading fuse and lock bits from firmware? on page 100 . 17.2.1 latching of fuses the fuse values are latched when the device enters programming mode and changes of the fuse values will have no effect until the part leaves programming mode. this does not apply to the eesave fuse which will take effect once it is programmed. the fuses are also latched on power-up in normal mode. 17.3 calibration bytes the signature area of the attiny13a contains two bytes of calibrati on data for the internal oscil- lator. the calibration data in the high byte of address 0x00 is for use with the oscillator set to 9.6 mhz operation. during reset, this byte is au tomatically written into the osccal register to ensure correct frequency of the oscillator. there is a separate calibration byte for the internal oscillator in 4.8 mhz mode of operation but this data is not loaded automatically. the hard ware always loads the 9.6 mhz calibration data during reset. to use separate calibration da ta for the oscillator in 4.8 mhz mode the osccal register must be updated by firmware. the calib ration data for 4.8 mhz operation is located in the high byte at address 0x 01 of the signature area. 17.4 signature bytes all atmel microcontrollers have a three-byte signature code which identifies the device. this code can be read in both serial and high-voltage programming mode, even when the device is locked. the three bytes reside in a separate address space. for the attiny13a the signature bytes are: ? 0x000: 0x1e (indicates manufactured by atmel). ? 0x001: 0x90 (indicates 1 kb flash memory). ? 0x002: 0x07 (indicates attiny13a device when 0x001 is 0x90). 17.5 page size table 17-5. no. of words in a page and no. of pages in the flash flash size page size pcword no. of pages pcpage pcmsb 512 words (1k byte) 16 words pc[3:0] 32 pc[8:4] 8 table 17-6. no. of words in a page and no. of pages in the eeprom eeprom size page size pcword no. of pages pcpage eeamsb 64 bytes 4 bytes eea[1:0] 16 eea[5:2] 5
106 8126d?avr?11/09 attiny13a 17.6 serial programming both the flash and eeprom memo ry arrays can be programmed us ing the serial spi bus while reset is pulled to gnd. the serial interface consists of pins sck, mosi (input) and miso (out- put). see figure 17-1 . figure 17-1. serial programming and verify note: if clocked by internal oscillator there is no need to connect a clock source to the clki pin. after reset is set low, the programming enable inst ruction needs to be executed first before program/erase operations can be executed. note: in table 17-7 above, the pin mapping for spi programming is listed. not all parts use the spi pins dedicated for the internal spi interface. when programming the eeprom, an auto-erase cycle is built into the self-timed programming operation (in the serial mode only) and there is no need to first execute the chip erase instruction. the chip erase operation turns the content of every memory location in both the program and eeprom arrays into 0xff. depending on cksel fuses, a va lid clock must be present. th e minimum low and high periods for the serial clock (sck) input are defined as follows: low: > 2 cpu clock cycles for f ck < 12 mhz, 3 cpu clock cycles for f ck >= 12 mhz high: > 2 cpu clock cycles for f ck < 12 mhz, 3 cpu clock cycles for f ck >= 12 mhz table 17-7. pin mapping serial programming symbol pins i/o description mosi pb0 i serial data in miso pb1 o serial data out sck pb2 i serial clock vcc gnd sck mis o mosi reset +1.8 - 5.5v pb0 pb1 pb2 pb5
107 8126d?avr?11/09 attiny13a 17.6.1 serial programming algorithm when writing serial data to the attiny13a, data is clocked on the rising edge of sck. when reading data from the atti ny13a, data is clocked on th e falling edge of sck. see figure 18-4 on page 122 and figure 18-3 on page 122 for timing details. to program and verify the attiny13a in the se rial programming mode, the following sequence is recommended (see four byte instruction formats in table 17-9 on page 108 ): 1. power-up sequence: apply power between v cc and gnd while reset and sck are set to ?0?. in some sys- tems, the programmer can not guarantee that sck is held low during power-up. in this case, reset must be given a positive pulse after sck has been set to ?0?. the pulse duration must be at least t rst (miniumum pulse widht of reset pin, see table 18-4 on page 120 and figure 19-60 on page 154 ) plus two cpu clock cycles. 2. wait for at least 20 ms and enable serial programming by sending the programming enable serial instru ction to pin mosi. 3. the serial programming instructions will not work if the communication is out of syn- chronization. when in sync. the second byte (0x53), will ec ho back when issuing the third byte of the programming enable instructio n. whether the echo is correct or not, all four bytes of the instruction must be tran smitted. if the 0x53 did not echo back, give reset a positive pulse and issue a new programming enable command. 4. the flash is programmed one page at a time . the memory page is loaded one byte at a time by supplying the 4 lsb of the add ress and data together with the load program memory page instruction. to ensure correct loading of the page, the data low byte must be loaded before data high byte is appli ed for a given address. the program memory page is stored by loading the write program memory page instruction with the 5 msb of the address. if polling ( rdy/bsy ) is not used, the user must wait at least t wd_flash before issuing the next page. (see table 17-8 on page 108 .) accessing the serial pro- gramming interface before the flash write op eration completes can result in incorrect programming. 5. a: the eeprom array is progra mmed one byte at a time by supplying the address and data together with t he appropriate write in struction. an eeprom memory location is first automatically erased before new data is written. if polling ( rdy/bsy ) is not used, the user must wait at least t wd_eeprom before issuing the next byte. (see table 17-8 on page 108 .) in a chip erased device, no 0xffs in the data file(s) need to be pro- grammed. b: the eeprom array is program med one page at a time. th e memory page is loaded one byte at a time by supplying the 2 lsb of the address and data together with the load eeprom memory page in struction. the eeprom memo ry page is stored by loading the write eeprom memory page inst ruction with the 4 msb of the address. when using eeprom page access only byte locations loaded with the load eeprom memory page instruction is altered. the remaining locations remain unchanged. if poll- ing ( rdy/bsy ) is not used, the used must wait at least t wd_eeprom before issuing the next page (see table 17-6 on page 105 ). in a chip erased device, no 0xff in the data file(s) need to be programmed. 6. any memory location can be verified by us ing the read instruct ion which returns the content at the selected addre ss at serial output miso. 7. at the end of the pr ogramming se ssion, reset can be set high to commence normal operation. 8. power-off sequence (if needed): set reset to ?1?. turn v cc power off.
108 8126d?avr?11/09 attiny13a . 17.6.2 serial programming instruction set the instruction set is described in table 17-9 . table 17-8. minimum wait delay before writing the next flash or eeprom location symbol minimum wait delay t wd_flash 4.5 ms t wd_eeprom 4.0 ms t wd_erase 9.0 ms t wd_fuse 4.5 ms table 17-9. serial programming instruction set instruction instruction format operation byte 1 byte 2 byte 3 byte4 programming enable 1010 1100 0101 0011 xxxx xxxx xxxx xxxx enable serial programming after reset goes low. chip erase 1010 1100 100x xxxx x xxx xxxx xxxx xxxx chip erase eeprom and flash. read program memory 0010 h 000 0000 000 a bbbb bbbb oooo oooo read h (high or low) data o from program memory at word address a:b . load program memory page 0100 h 000 000x xxxx xxxx bbbb iiii iiii write h (high or low) data i to program memory page at word address b . data low byte must be loaded before data high byte is applied within the same address. write program memory page 0100 1100 0000 000 a bbbb xxxx xxxx xxxx write program memory page at address a:b . read eeprom memory 1010 0000 000x xxxx xx bb bbbb oooo oooo read data o from eeprom memory at address b . write eeprom memory 1100 0000 000x xxxx xx bb bbbb iiii iiii write data i to eeprom memory at address b . load eeprom memory page (page access) 1100 0001 0000 0000 0000 00 bb iiii iiii load data i to eeprom memory page buffer. after data is loaded, program eeprom page. write eeprom memory page (page access) 1100 0010 00xx xxxx xx bb bb 00 xxxx xxxx write eeprom page at address b . read lock bits 0101 1000 0000 0000 xxxx xxxx xx oo oooo read lock bits. ?0? = programmed, ?1? = unprogrammed. see table 17-1 on page 103 for details. write lock bits 1010 1100 111x xxxx xxxx xxxx 11 ii iiii write lock bits. set bits = ?0? to program lock bits. see table 17-1 on page 103 for details.
109 8126d?avr?11/09 attiny13a note: a = address high bits, b = address low bits, h = 0 - low byte, 1 - high byte, o = data out, i = data in, x = don?t care 17.7 high-voltage serial programming this section describes how to program and verify flash program memory, eeprom data mem- ory, lock bits and fuse bits in the attiny13a. figure 17-2. high-voltage serial programming read fuse byte 0101 h 000 0000 h 000 xxxx xxxx oooo oooo read fuse low/high byte. bit ?0? = programmed, ?1? = unprogrammed. see ?fuse bytes? on page 104 for details. write fuse byte 1010 1100 1010 h 000 xxxx xxxx iiii iiii set fuse low/high byte . set bit to ?0? to program, ?1? to unprogram. see ?fuse bytes? on page 104 for details. read signature byte 0011 0000 000x xxxx xxxx xx bb oooo oooo read signature byte o at address b . read calibration byte 0011 1000 000x xxxx 0000 000b oooo oooo read calibration byte. see ?calibration bytes? on page 105 poll rdy/bsy 1111 0000 0000 0000 xxxx xxxx xxxx xxx o if o = ?1?, a programming operation is still busy. wait until this bit returns to ?0? before applying another command. table 17-9. serial programming instruction set (continued) instruction instruction format operation byte 1 byte 2 byte 3 byte4 vcc gnd sdo sii sdi (reset) +1.8 - 5.5v pb0 pb1 pb2 pb5 +11.5 - 12.5v pb3 sci
110 8126d?avr?11/09 attiny13a the minimum period for the serial clock input (sci) during high-voltage serial programming is 220 ns. 17.7.1 high-voltage serial programming algorithm to program and verify the attiny13a in the hi gh-voltage serial programming mode, the follow- ing sequence is recommended (see instruction formats in table 17-13 on page 111 ): the following algorithm puts the device in high-voltage serial programming mode: 1. set prog_enable pins listed in table 17-11 to ?000?, reset pin to ?0? and vcc to 0v. 2. apply 4.5 - 5.5v between vcc and gnd. ensu re that vcc reaches at least 1.8v within the next 20s. 3. wait 20 - 60s, and apply 11.5 - 12.5v to reset. 4. keep the prog_enable pins unchanged for at least 10s after the high-voltage has been applied to ensure the prog_enable signature has been latched. 5. release the prog_enable[2] pin to avoid dr ive contention on the prog_enable[2]/sdo pin. 6. wait at least 300s before giving any serial instructions on sdi/sii. 7. exit programming mode by po wer the device down or by bringing reset pin to 0v. if the rise time of the vcc is unable to fulfill the requirements listed ab ove, the following alterna- tive algorithm can be used. 1. set prog_enable pins listed in table 17-11 to ?000?, reset pin to ?0? and vcc to 0v. 2. apply 4.5 - 5.5v between vcc and gnd. 3. monitor vcc, and as soon as vcc reaches 0.9 - 1.1v, ap ply 11.5 - 12.5v to reset. 4. keep the prog_enable pins unchanged for at least 10s after thehigh-voltage has been applied to ensure the prog_enable signature has been latched. 5. release the prog_enable[2] pin to avoid dr ive contention on the prog_enable[2]/sdo pin. table 17-10. pin name mapping signal name in high-voltage serial programming mode pin name i/o function sdi pb0 i serial data input sii pb1 i serial instruction input sdo pb2 o serial data output sci pb3 i serial clock input (min. 220ns period) table 17-11. pin values used to enter programming mode pin symbol value sdi prog_enable[0] 0 sii prog_enable[1] 0 sdo prog_enable[2] 0
111 8126d?avr?11/09 attiny13a 6. wait until vcc actually reaches 4.5 - 5.5v before giving any serialinstructions on sdi/sii. 7. exit programming mode by po wer the device down or by bringing reset pin to 0v. 17.7.2 high-voltage serial programming instruction set the instruction set is described in table 17-13 . table 17-12. high-voltage reset characteristics supply voltage reset pin high-voltage threshold minimum high-volt age period for latching prog_enable v cc v hvrst t hvrst 4.5v 12v 100 ns 5.5v 12 100 ns table 17-13. high-voltage serial programming instruction set for attiny13a instruction instruction format operation remarks instr.1/5 instr.2/6 instr.3 instr.4 chip erase sdi sii sdo 0_1000_0000_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx wait after instr.3 until sdo goes high for the chip erase cycle to finish. load ?write flash? command sdi sii sdo 0_0001_0000_00 0_0100_1100_00 x_xxxx_xxxx_xx enter flash programming code. load flash page buffer sdi sii sdo 0_ bbbb _ bbbb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_ eeee _ eeee _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_ dddd _ dddd _00 0_0011_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1101_00 x_xxxx_xxxx_xx repeat after instr. 1 - 5 until the entire page buffer is filled or until all data within the page is filled. see note 1. sdi sii sdo 0_0000_0000_00 0_0111_1100_00 x_xxxx_xxxx_xx instr 5. load flash high address and program page sdi sii sdo 0_0000_000 a _00 0_0001_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx wait after instr 3 until sdo goes high. repeat instr. 2 - 3 for each loaded flash page until the entire flash or all data is programmed. repeat instr. 1 for a new 256 byte page. see note 1. load ?read flash? command sdi sii sdo 0_0000_0010_00 0_0100_1100_00 x_xxxx_xxxx_xx enter flash read mode. read flash low and high bytes sdi sii sdo 0_ bbbb _ bbbb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_0000_000 a _00 0_0001_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 q _ qqqq _ qqq x_xx repeat instr. 1, 3 - 6 for each new address. repeat instr. 2 for a new 256 byte page. sdi sii sdo 0_0000_0000_00 0_0111_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1100_00 p _ pppp _ ppp x_xx instr 5 - 6. load ?write eeprom? command sdi sii sdo 0_0001_0001_00 0_0100_1100_00 x_xxxx_xxxx_xx enter eeprom programming mode.
112 8126d?avr?11/09 attiny13a load eeprom page buffer sdi sii sdo 0_00 bb _ bbbb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_ eeee _ eeee _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1101_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx repeat instr. 1 - 4 until the entire page buffer is filled or until all data within the page is filled. see note 2. program eeprom page sdi sii sdo 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx wait after instr. 2 until sdo goes high. repeat instr. 1 - 2 for each loaded eeprom page until the entire eeprom or all data is programmed. write eeprom byte sdi sii sdo 0_00 bb _ bbbb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_ eeee _ eeee _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1101_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx repeat instr. 1 - 5 for each new address. wait after instr. 5 until sdo goes high. see note 3. sdi sii sdo 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx instr. 5 load ?read eeprom? command sdi sii sdo 0_0000_0011_00 0_0100_1100_00 x_xxxx_xxxx_xx enter eeprom read mode. read eeprom byte sdi sii sdo 0_ bbbb _ bbbb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_ aaaa _ aaaa _00 0_0001_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 q _ qqqq _ qqq 0_00 repeat instr. 1, 3 - 4 for each new address. repeat instr. 2 for a new 256 byte page. write fuse low bits sdi sii sdo 0_0100_0000_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_ a987 _ 6543 _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx wait after instr. 4 until sdo goes high. write a - 3 = ?0? to program the fuse bit. write fuse high bits sdi sii sdo 0_0100_0000_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_000 f _ edcb _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1100_00 x_xxxx_xxxx_xx wait after instr. 4 until sdo goes high. write f - b = ?0? to program the fuse bit. write lock bits sdi sii sdo 0_0010_0000_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_00 21 _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx wait after instr. 4 until sdo goes high. write 2 - 1 = ?0? to program the lock bit. read fuse low bits sdi sii sdo 0_0000_0100_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 a _ 9876 _ 543 x_xx reading a - 3 = ?0? means the fuse bit is programmed. read fuse high bits sdi sii sdo 0_0000_0100_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1010_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1110_00 x_xx fe _ dcb x_xx reading f - b = ?0? means the fuse bit is programmed. read lock bits sdi sii sdo 0_0000_0100_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1100_00 x_xxxx_x 21 x_xx reading 2, 1 = ?0? means the lock bit is programmed. table 17-13. high-voltage serial programming instruction set for attiny13a (continued) instruction instruction format operation remarks instr.1/5 instr.2/6 instr.3 instr.4
113 8126d?avr?11/09 attiny13a note: a = address high bits, b = address low bits, d = data in high bits, e = data in low bits, p = data out high bits, q = data out low bits, x = don?t care, 1 = lock bit1, 2 = lock bit2, 3 = cksel0 fuse, 4 = cksel1 fuse, 5 = sut0 fuse, 6 = sut1 fuse, 7 = ckdiv8, fuse, 8 = wdton fuse, 9 = eesave fuse, a = spien fuse, b = rstdisbl fuse, c = bodlevel0 fuse, d = bodlevel1 fuse, e = monen fuse, f = selfprgen fuse note: the eeprom is written page-wise. but on ly the bytes that are load ed into the page are actually written to the eeprom. page - wise eeprom access is more efficient when multiple bytes are to be written to the same page. note that auto-erase of eeprom is not available in high-voltage seri al programming, only in spi programming. 17.8 considerations for efficient programming the loaded command and address are retained in the device during programming. for efficient programming, the following should be considered. ? the command needs only be loaded once when writing or reading multiple memory locations. ? skip writing the data value 0xff that is th e contents of the entire eeprom (unless the eesave fuse is programmed) and flash after a chip erase. ? address high byte needs only be loaded before programming or reading a new 256 word window in flash or 256 byte eeprom. this consideration also applies to signature bytes reading. 17.8.1 chip erase the chip erase will erase the flash and eeprom (1) memories plus lock bits. the lock bits are not reset until the program memory has been completely erased. the fuse bits are not changed. a chip erase must be performed before the flash and/or eeprom are re-programmed. 1. load command ?chip erase? (see table 17-13 on page 111 ). 2. wait after instr. 3 until sdo goes high for the ?chip erase? cycle to finish. 3. load command ?no operation?. note: 1. the eeprom memory is preserved during chip erase if the eesave fuse is programmed. read signature bytes sdi sii sdo 0_0000_1000_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_00 bb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 q _ qqqq _ qqq x_xx repeats instr 2 4 for each signature byte address. read calibration byte sdi sii sdo 0_0000_1000_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_000b_00 0_0000_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1100_00 p _ pppp _ ppp x_xx load ?no operation? command sdi sii sdo 0_0000_0000_00 0_0100_1100_00 x_xxxx_xxxx_xx table 17-13. high-voltage serial programming instruction set for attiny13a (continued) instruction instruction format operation remarks instr.1/5 instr.2/6 instr.3 instr.4
114 8126d?avr?11/09 attiny13a 17.8.2 programming the flash the flash is organized in pages, see table 17-9 on page 108 . when programming the flash, the program data is latched into a page buffer. this allows one page of program data to be pro- grammed simultaneously. the following procedure describes how to program the entire flash memory: 1. load command ?write flash? (see table 17-13 on page 111 ). 2. load flash page buffer. 3. load flash high address and program page. wait after instr. 3 until sdo goes high for the ?page programming? cycle to finish. 4. repeat 2 through 3 until the entire flash is programmed or until all data has been programmed. 5. end page programming by loading command ?no operation?. when writing or reading serial data to the attiny13a, data is clocked on the rising edge of the serial clock, see figure 17-4 on page 115 , figure 18-5 on page 123 and table 18-10 on page 123 for details. figure 17-3. addressing the flash which is organized in pages program memory word address within a page page address within the flash instruction word pag e pcword[pagemsb:0]: 00 01 02 pageend pag e pcword pcpage pcmsb pagemsb program counter
115 8126d?avr?11/09 attiny13a figure 17-4. high-voltage serial programming waveforms 17.8.3 programming the eeprom the eeprom is organized in pages, see table 18-9 on page 122 . when programming the eeprom, the data is latc hed into a page buffer. this allo ws one page of data to be pro- grammed simultaneously. the programming algorithm for the eeprom data memory is as follows (refer to table 17-13 on page 111 ): 1. load command ?write eeprom?. 2. load eeprom page buffer. 3. program eeprom page. wait after instr. 2 until sdo goes high for the ?page pro- gramming? cycle to finish. 4. repeat 2 through 3 until the entire eeprom is prog rammed or until all data has been programmed. 5. end page programming by loading command ?no operation?. 17.8.4 reading the flash the algorithm for reading the flash memory is as follows (refer to table 17-13 on page 111 ): 1. load command "read flash". 2. read flash low and high bytes. the contents at the selected address are available at serial output sdo. 17.8.5 reading the eeprom the algorithm for reading the eeprom memory is as follows (refer to table 17-13 on page 111 ): 1. load command ?read eeprom?. 2. read eeprom byte. the contents at the se lected address are available at serial out- put sdo. 17.8.6 programming and reading the fuse and lock bits the algorithms for programming and reading the fu se low/high bits and lock bits are shown in table 17-13 on page 111 . msb msb msb lsb lsb lsb 012345678910 sdi pb0 sii pb1 sdo pb2 sci pb3
116 8126d?avr?11/09 attiny13a 17.8.7 reading the signature bytes and calibration byte the algorithms for reading the signature bytes and calibration byte are shown in table 17-13 on page 111 . 17.8.8 power-off sequence set sci to ?0?. set reset to ?1?. turn v cc power off.
117 8126d?avr?11/09 attiny13a 18. electrical characteristics 18.1 absolute maximum ratings* 18.2 dc characteristics operating temperature....................................-55 ? c to +125 ? c *notice: stresses beyond those listed under ?absolute maximum ratings? may cause permanent dam- age to the device. this is a stress rating only and functional operation of the device at these or other conditions beyond those indicated in the operational sections of th is specification is not implied. exposure to absolute maximum rating conditions for extended periods may affect device reliability. storage temperature ..................................... -65c to +150c voltage on any pin except reset with respect to ground ................................-0.5v to v cc +0.5v voltage on reset with respect to ground......-0.5v to +13.0v maximum operating voltage ............................................ 6.0v dc current per i/o pin ............................................... 40.0 ma dc current v cc and gnd pins................................ 200.0 ma table 18-1. dc characteristics, t a = -40 ? c to 85 ? c symbol parameter condition min typ max units v il input low voltage, any pin as i/o v cc = 1.8v - 2.4v -0.5 0.2v cc (1) v v cc = 2.4v - 5.5v -0.5 0.3v cc (1) v input low voltage, reset pin as reset (2) v cc = 1.8v - 5.5 -0.5 0.2v cc (1) v v ih input high voltage, any pin as i/o v cc = 1.8v - 2.4v 0.7v cc (3) v cc + 0.5 v v cc = 2.4v - 5.5v 0.6v cc (3) v cc + 0.5 v input high voltage, reset pin as reset (2) v cc = 1.8v - 5.5v 0.9v cc (3) v cc + 0.5 v v ol output low voltage, pins pb0 and pb1 (4) i ol = 20 ma, v cc = 5v 0.8 v i ol = 10 ma, v cc = 3v 0.6 v output low voltage, pins pb2, pb3 and pb4 (4) i ol = 10 ma, v cc = 5v 0.8 v i ol = 5 ma, v cc = 3v 0.6 v v oh output high voltage, pins pb0 and pb1 (5) i oh = -20 ma, v cc = 5v 4.0 v i oh = -10 ma, v cc = 3v 2.3 v output high voltage, pins pb2, pb3 and pb4 (5) i oh = -10 ma, v cc = 5v 4.2 v i oh = -5 ma, v cc = 3v 2.5 v i lil input leakage current i/o pin v cc = 5.5 v, pin low -1 1 a i lih input leakage current i/o pin v cc = 5.5 v, pin high -1 1 a r pu pull-up resistor, i/o pin v cc = 5.5 v, input low 20 50 k pull-up resistor, reset pin v cc = 5.5 v, input low 30 80 k
118 8126d?avr?11/09 attiny13a notes: 1. ?max? means the highest value where the pin is guaranteed to be read as low. 2. not tested in production. 3. ?min? means the lowest value where th e pin is guaranteed to be read as high. 4. although each i/o port can under non-trans ient, steady state conditions sink more t han the test conditions, the sum of all i ol (for all ports) should not exceed 60 ma. if i ol exceeds the test condition, v ol may exceed the related specification. pins are not guaranteed to sink current great er than the listed test condition. 5. although each i/o port can under non-transient, steady state co nditions source more than the test conditions, the sum of all i oh (for all ports) should not exceed 60 ma. if i oh exceeds the test condition, v oh may exceed the related specification. pins are not guaranteed to source current greater than the listed test condition. 6. measured with all i/o modules turned off (prr = 0xff). 18.3 speed grades the maximum operating frequency of the device depends on supply voltage, v cc. as shown in figure 18-1 , the relationship between maximum frequency and v cc is linear in the range of 1.8v to 4.5v. figure 18-1. maximum frequency vs. v cc i cc supply current, active mode (6) f = 1mhz, v cc = 2v 0.2 0.35 ma f = 4mhz, v cc = 3v 1.2 1.8 ma f = 8mhz, v cc = 5v 3.6 6 ma supply current, idle mode f = 1mhz, v cc = 2v 0.03 0.2 ma f = 4mhz, v cc = 3v 0.2 1 ma f = 8mhz, v cc = 5v 0.7 3 ma supply current, power-down mode wdt enabled, v cc = 3v 3.9 10 a wdt disabled, v cc = 3v 0.15 2 a table 18-1. dc characteristics, t a = -40 ? c to 85 ? c (continued) symbol parameter condition min typ max units 4 mhz 1.8v 5.5v 4.5v 20 mhz
119 8126d?avr?11/09 attiny13a 18.4 clock characteristics 18.4.1 calibrated internal rc oscillator accuracy it is possible to manua lly calibrate the internal oscillator to be more accu rate than def ault factory calibration. note that the osc illator frequency depend s on temperat ure and voltage. voltage and temperature characteristics can be found in figure 19-46 on page 147 , figure 19-47 on page 148 , figure 19-48 on page 148 , figure 19-49 on page 149 , figure 19-50 on page 149 , and fig- ure 19-51 on page 150 . notes: 1. accuracy of oscillator frequency at calibrat ion point (fixed temperature and fixed voltage). 18.4.2 external clock drive figure 18-2. external clock drive waveform table 18-2. calibration accuracy of internal rc oscillator calibration method target frequency v cc temperature accuracy at given voltage & temperature (1) factory calibration 4.8 / 9.6 mhz 3v 25 ? c10% user calibration fixed frequency within: 4 ? 5 mhz / 8 ? 10 mhz fixed voltage within: 1.8v ? 5.5v fixed temperature within: -40 ? c ? 85 ? c 2% v il1 v ih1 table 18-3. external clock drive symbol parameter v cc = 1.8 - 5.5v v cc = 2.7 - 5.5v v cc = 4.5 - 5.5v units min. max. min. max. min. max. 1/t clcl clock frequency 0 4 0 10 0 20 mhz t clcl clock period 250 100 50 ns t chcx high time 100 40 20 ns t clcx low time 100 40 20 ns t clch rise time 2.0 1.6 0.5 s t chcl fall time 2.0 1.6 0.5 s t clcl change in period fr om one clock cycle to the next 2 2 2 %
120 8126d?avr?11/09 attiny13a 18.5 system and reset characteristics note: 1. when reset pin used as reset (not as i/o). 2. not tested in production. 18.5.1 enhanced power-on reset note: 1. values are guidelines, only. 2. threshold where device is released from reset when voltage is rising. 3. the power-on reset will not work unless the supply voltage has been below v poa . 18.5.2 brown-out detection note: 1. v bot may be below nominal minimum operating voltage for some devices. for devices where this is the case, the device is tested down to v cc = v bot during the production test. this guar- antees that a brown-out reset will occur before v cc drops to a voltage where correct operation of the microcontroller is no longer guaranteed. table 18-4. reset, brown-out, and internal voltage characteristics symbol parameter condi tion min typ max units v rst reset pin threshold voltage 0.2 v cc 0.9v cc v t rst minimum pulse width on reset pin (1) v cc = 1.8v v cc = 3v v cc = 5v 2000 700 400 2500 2500 2500 ns v hyst brown-out detector hysteresis (2) 50 mv t bod min pulse width on brown-out reset (2) 2s v bg internal bandgap reference voltage (2) v cc = 5v t a = 25c 1.0 1.1 1.2 v t bg internal bandgap reference start-up time (2) v cc = 5v t a = 25c 40 70 s i bg internal bandgap reference current consumption (2) v cc = 5v t a = 25c 15 a table 18-5. characteristics of enhanced power-on reset. t a = -40 ? 85 ? c symbol parameter min (1) typ (1) max (1) units v por release threshold of power-on reset (2) 1.1 1.4 1.6 v v poa activation threshold of power-on reset (3) 0.6 1.3 1.6 v sr on power-on slope rate 0.01 v/ms table 18-6. v bot vs. bodlevel fuse coding bodlevel [1:0] fuses min (1) typ (1) max (1) units 11 bod disabled 10 1.7 1.8 2.0 v 01 2.5 2.7 2.9 00 4.1 4.3 4.5
121 8126d?avr?11/09 attiny13a 18.6 analog comparat or characteristics note: all parameters are based on simulation re sults and they are not tested in production 18.7 adc characteristics table 18-7. analog comparator characteristics, t a = -40 ? c - 85 ? c symbol parameter condition min typ max units v aio input offset voltage v cc = 5v, v in = v cc / 2 < 10 40 mv i lac input leakage current v cc = 5v, v in = v cc / 2 -50 50 na t apd analog propagation delay (from saturation to slight overdrive) v cc = 2.7v 750 ns v cc = 4.0v 500 analog propagation delay (large step change) v cc = 2.7v 100 v cc = 4.0v 75 t dpd digital propagation delay v cc = 1.8v - 5.5 1 2 clk table 18-8. adc characteristics, single ended channels. t a = -40 ? c - 85 ? c symbol parameter condition min typ max units resolution 10 bits absolute accuracy (including inl, dnl, and quantization, gain and offset errors) v ref = 4v, v cc = 4v, adc clock = 200 khz 3lsb v ref = 4v, v cc = 4v, adc clock = 1 mhz 4lsb v ref = 4v, v cc = 4v, adc clock = 200 khz, noise reduction mode 2.5 lsb v ref = 4v, v cc = 4v, adc clock = 1 mhz, noise reduction mode 3.5 lsb integral non-linearity (inl) (accuracy after offset and gain calibration) v ref = 4v, v cc = 4v, adc clock = 200 khz 1lsb differential non-linearity (dnl) v ref = 4v, v cc = 4v, adc clock = 200 khz 0.5 lsb gain error v ref = 4v, v cc = 4v, adc clock = 200 khz 3.5 lsb offset error v ref = 4v, v cc = 4v, adc clock = 200 khz 2.5 lsb conversion time free running conversion 13 260 s clock frequency 50 1000 khz v in input voltage gnd v ref v input bandwidth 38.5 khz v int internal voltage reference 1.0 1.1 1.2 v r ain analog input resistance 100 m
122 8126d?avr?11/09 attiny13a 18.8 serial programming characteristics figure 18-3. serial programming timing figure 18-4. serial programming waveform note: 1. 2 t clcl for f ck < 12 mhz, 3 t clcl for f ck >= 12 mhz table 18-9. serial programming characteristics, t a = -40 ? c to 85 ? c symbol parameter condition min typ max units 1/t clcl oscillator frequency v cc = 1.8 ? 5.5v 01mhz t clcl oscillator period 1000 ns 1/t clcl oscillator frequency v cc = 2.7 ? 5.5v 09.6mhz t clcl oscillator period 104 ns 1/t clcl oscillator frequency v cc = 4.5 ? 5.5v 020mhz t clcl oscillator period 50 ns t shsl sck pulse width high v cc = 1.8 ? 5.5v 2 t clcl (1) ns t slsh sck pulse width low 2 t clcl (1) ns t ovsh mosi setup to sck high t clcl ns t shox mosi hold after sck high 2 t clcl ns mosi miso sck t ovsh t shsl t slsh t shox msb msb lsb lsb serial clock input (sck) serial data input (mosi) (miso) sample serial data output
123 8126d?avr?11/09 attiny13a 18.9 high-voltage serial programming characteristics figure 18-5. high-voltage serial programming timing table 18-10. high-voltage serial prog ramming characteristics t a = 25 ? c, v cc = 5.0v 10% (unless otherwise noted) symbol parameter min typ max units t shsl sci (pb3) pulse width high 110 ns t slsh sci (pb3) pulse width low 110 ns t ivsh sdi (pb0), sii (pb1) vali d to sci (pb3) high 50 ns t shix sdi (pb0), sii (pb1) hold after sci (pb3) high 50 ns t shov sci (pb3) high to sdo (pb2) valid 16 ns t wlwh_pfb wait after instr. 3 for write fuse bits 2.5 ms sdi (pb0), sii (pb1) sdo (pb2) sci (pb3) t ivsh t shsl t slsh t shix t shov
124 8126d?avr?11/09 attiny13a 19. typical characteristics the data contained in this section is largely ba sed on simulations and characterization of similar devices in the same process and design methods. thus, the data should be treated as indica- tions of how the part will behave. the following charts show typical behavior. t hese figures are not tested during manufacturing. during characterisation devices are operated at fr equencies higher than test limits but they are not guaranteed to function properly at frequenc ies higher than the ordering code indicates. all current consumption measurements are perfor med with all i/o pins configured as inputs and with internal pull-ups enabled. current consumption is a function of several factors such as oper- ating voltage, operating frequency, loading of i/o pins, switching rate of i/o pins, code executed and ambient temperature. the dominating factors are operating voltage and frequency. a sine wave generator with rail-to-rail output is used as clock source but current consumption in power-down mode is independent of clock select ion. the difference between current consump- tion in power-down mode with watchdog timer enabled and power-down mode with watchdog timer disabled represents the differenti al current drawn by the watchdog timer. the current drawn from pins with a capacitive lo ad may be estimated (for one pin) as follows: where v cc = operating voltage, c l = load capacitance and f sw = average switching frequency of i/o pin. 19.1 supply current of i/o modules using table 19-1 , the typical characteristics of this se ction and the equation given one can cal- culate the additional current co nsumption for peripheral modules in active and idle mode. peripheral modules are enabled and disabled via c ontrol bits in the power reduction register. see ?power reduction register? on page 31 for details. 19.1.1 example estimate current consumption in idle mode, with timer/counter0 and adc enabled, the device running at 2v and with 1mhz external clock. from figure 19-7 on page 128 we find idle supply current i cc = 0.03ma. using figure 19-55 on page 152 we find adc supply current i adc = 0.18ma, and using table 19-1 we find timer/counter0 supply current i tc0 = 0.004ma. the total current consumption in idle mode is therefore i cctot = 0.214ma, approximately 0.21ma. i cp v cc c l f sw table 19-1. additional current consumption (absolute) for peripherals prr bit typical numbers v cc = 2v, f = 1mhz v cc = 3v, f = 4mhz v cc = 5v, f = 8mhz prtim0 4 a 25 a 115 a pradc 180 a 260 a 460 a
125 8126d?avr?11/09 attiny13a 19.2 active supply current figure 19-1. active supply current vs. frequency (0.1 - 1.0 mhz) figure 19-2. active supply current vs . frequency (1 - 20 mhz) active supply current vs. low frequency (prr=0xff) 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 frequency (mhz) i cc (ma) active supply current vs. frequency (prr=0xff) 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 2 4 6 8 10 12 0 2 4 6 8 101214161820 frequency (mhz) i cc (ma)
126 8126d?avr?11/09 attiny13a figure 19-3. active supply current vs. v cc (internal rc o scillator, 9.6 mhz) figure 19-4. active supply current vs. v cc (internal rc o scillator, 4.8 mhz) active supply current vs. v cc internal rc oscillator, 9.6 mhz 85 c 25 c -40 c 0 1 2 3 4 5 6 7 8 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma) active supply current vs. v cc internal rc oscillator, 4.8 mhz 85 c 25 c -40 c 0 0.5 1 1.5 2 2.5 3 3.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma)
127 8126d?avr?11/09 attiny13a figure 19-5. active supply current vs. v cc (internal wdt o scillator, 128 khz) figure 19-6. active supply current vs. v cc (32 khz external clock) active supply current vs. v cc internal wd oscillator, 128 khz 85 c 25 c -40 c 0 0.02 0.04 0.06 0.08 0.1 0.12 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma) active supply current vs. v cc 32 khz external clock, prr = 0xff 85 c 25 c -40 c 0 0.005 0.01 0.015 0.02 0.025 0.03 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma)
128 8126d?avr?11/09 attiny13a 19.3 idle supply current figure 19-7. idle supply current vs. frequency (0.1 - 1.0 mhz) figure 19-8. idle supply current vs. frequency (1 - 20 mhz) idle supply current vs. low frequency (prr=0xff) 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0.02 0.04 0.06 0.08 0.1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 frequency (mhz) i cc (ma) idle supply current vs. frequency (prr=0xff) 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 0 0.5 1 1.5 2 0 2 4 6 8 101214161820 frequency (mhz) i cc (ma) 1.8 v
129 8126d?avr?11/09 attiny13a figure 19-9. idle supply current vs. v cc (internal rc o scillator, 9.6 mhz) figure 19-10. idle supply current vs. v cc (internal rc o scillator, 4.8 mhz) idle supply current vs. v cc internal rc oscillator, 9.6 mhz 85 c 25 c -40 c 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma) idle supply current vs. v cc internal rc oscillator, 4.8 mhz 85 c 25 c -40 c 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma)
130 8126d?avr?11/09 attiny13a figure 19-11. idle supply current vs. v cc (internal rc o scillator, 128 khz) figure 19-12. idle supply current vs. v cc (32 khz external clock) idle supply current vs. v cc internal wd oscillator, 128 khz 85 c 25 c -40 c 0 0.005 0.01 0.015 0.02 0.025 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma) idle supply current vs. v cc 32 khz external oscillator, prr=0xff 85 c 25 c -40 c 0 0.001 0.002 0.003 0.004 0.005 0.006 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma)
131 8126d?avr?11/09 attiny13a 19.4 power-down supply current figure 19-13. power-down supply current vs. v cc (watchdog timer disabled) figure 19-14. power-down supply current vs. v cc (watchdog timer enabled) power-down supply current vs. v cc watchdog timer disabled 85 c 25 c -40 c 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ua) power-down supply current vs. v cc watchdog timer enabled 85 c 25 c -40 c 0 1 2 3 4 5 6 7 8 9 10 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ua)
132 8126d?avr?11/09 attiny13a 19.5 pin pull-up figure 19-15. i/o pin pull-up resistor current vs. input voltage (v cc = 5v) figure 19-16. i/o pin pull-up resistor current vs. input voltage (v cc = 2.7v) i/o pin pull-up resistor current vs. input voltage v cc = 5v 85 c 25 c -40 c 0 20 40 60 80 100 120 140 160 0123456 v op (v) i op (ua) i/o pin pull-up resistor current vs. input voltage v cc = 2.7v 85 c 25 c -40 c 0 10 20 30 40 50 60 70 80 90 0 0.5 1 1.5 2 2.5 3 v op (v) i op (ua)
133 8126d?avr?11/09 attiny13a figure 19-17. reset pull-up resistor curr ent vs. reset pin voltage (v cc = 5v) figure 19-18. reset pull-up resistor curr ent vs. reset pin voltage (v cc = 2.7v) reset pull-up resistor current vs. reset pin voltage v cc = 5v 85 c 25 c -40 c 0 20 40 60 80 100 120 140 0123456 v reset (v) i reset (ua) reset pull-up resistor current vs. reset pin voltage v cc = 2.7v 85 c 25 c -40 c 0 10 20 30 40 50 60 70 0 0.5 1 1.5 2 2.5 3 v reset (v) i reset (ua)
134 8126d?avr?11/09 attiny13a 19.6 pin driver strength figure 19-19. i/o pin output voltage vs. source current (low power pins, v cc = 5v) figure 19-20. i/o pin output voltage vs. source current (low power pins, v cc = 3v) i/o pin output voltage vs. source current low power pins, v cc = 5v 85 c 25 c -40 c 4 4.2 4.4 4.6 4.8 5 5.2 0 2 4 6 8 101214161820 i oh (ma) v oh (v) i/o pin output voltage vs. source current low power pins, v cc = 3v 85 c 25 c -40 c 0 0.5 1 1.5 2 2.5 3 3.5 012345678910 i oh (ma) v oh (v)
135 8126d?avr?11/09 attiny13a figure 19-21. i/o pin output voltage vs. source current (low power pins, v cc = 1.8v) figure 19-22. i/o pin output voltage vs. sink current (low power pins, v cc = 5v) i/o pin output voltage vs. source current low power pins, v cc = 1.8v 85 c 25 c -40 c 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 i oh (ma) v oh (v) i/o pin output voltage vs. sink current low power pins, v cc = 5v 85 c 25 c -40 c 0 0.2 0.4 0.6 0.8 1 1.2 02468101214161820 i ol (ma) v ol (v)
136 8126d?avr?11/09 attiny13a figure 19-23. i/o pin output voltage vs. sink current (low power pins, v cc = 3v) figure 19-24. i/o pin output voltage vs. sink current (low power pins, v cc = 1.8v) i/o pin output voltage vs. sink current low power pins, v cc = 3v 85 c 25 c -40 c 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 012345678910 i ol (ma) v ol (v) i/o pin output voltage vs. sink current low power pins, v cc = 1.8v 85 c 25 c -40 c 0 0.5 1 1.5 2 2.5 3 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 i ol (ma) v ol (v)
137 8126d?avr?11/09 attiny13a figure 19-25. i/o pin output voltage vs. source current (v cc = 5v) figure 19-26. i/o pin output voltage vs. source current (v cc = 3v) i/o pin output voltage vs. source current v cc = 5v 85 c 25 c -40 c 4 4.2 4.4 4.6 4.8 5 5.2 i oh (ma) v oh (v) i/o pin output voltage vs. source current v cc = 3v 85 c 25 c -40 c 1.5 1.7 1.9 2.1 2.3 2.5 2.7 2.9 3.1 012345678910 i oh (ma) v oh (v)
138 8126d?avr?11/09 attiny13a figure 19-27. i/o pin output voltage vs. source current (v cc = 1.8v) figure 19-28. i/o pin output voltage vs. sink current (v cc = 5v) i/o pin output voltage vs. source current v cc = 1.8v 85 c 25 c -40 c 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 0123456 i oh (ma) v oh (v) i/o pin output voltage vs. sink current v cc = 5v 85 c 25 c -40 c 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0 2 4 6 8 101214161820 i ol (ma) v ol (v)
139 8126d?avr?11/09 attiny13a figure 19-29. i/o pin output voltage vs. sink current (v cc = 3v) figure 19-30. i/o pin output voltage vs. sink current (v cc = 1.8v) i/o pin output voltage vs. sink current v cc = 3v 85 c 25 c -40 c 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 012345678910 i ol (ma) v ol (v) i/o pin output voltage vs. sink current v cc = 1.8v 85 c 25 c -40 c 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0123456 i ol (ma) v ol (v)
140 8126d?avr?11/09 attiny13a figure 19-31. reset pin as i/o - output voltage vs. source current figure 19-32. reset pin as i/o - output voltage vs. source current output voltage vs. source current reset pin as i/o 5.0 v 3.0 v 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 i oh (ma) v oh (v) 1.8 v output voltage vs. sink current reset pin as i/o 5.0 v 3.0 v 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 0 0.5 1 1.5 2 i ol (ma) v ol (v) 1.8 v
141 8126d?avr?11/09 attiny13a 19.7 pin thresholds and hysteresis figure 19-33. i/o pin input threshold voltage vs. v cc (v ih , i/o pin read as '1') figure 19-34. i/o pin input threshold voltage vs. v cc (v il , i/o pin read as '0') i/o pin input threshold voltage vs. vcc v ih , i/o pin read as '1' 85 c 25 c -40 c 0 0.5 1 1.5 2 2.5 3 3.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) threshold (v) i/o pin input threshold voltage vs. vcc v il , i/o pin read as '0' 85 c 25 c -40 c 0 0.5 1 1.5 2 2.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) threshold (v)
142 8126d?avr?11/09 attiny13a figure 19-35. i/o pin input hysteresis vs. v cc figure 19-36. reset pin as i/o - input threshold voltage vs. v cc (v ih , reset pin read as '1') i/o pin input hysteresis vs. v cc 85 c 25 c -40 c 0 0.1 0.2 0.3 0.4 0.5 0.6 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) input hysteresis (v) reset pin as i/o threshold voltage vs. vcc v ih , reset read as '1' 85 c 25 c -40 c 0 0.5 1 1.5 2 2.5 3 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) threshold (v)
143 8126d?avr?11/09 attiny13a figure 19-37. reset pin as i/o - input threshold voltage vs. v cc (v il , reset pin read as '0') figure 19-38. reset pin as i/o - pin hysteresis vs. v cc reset pin as i/o threshold voltage vs. vcc v il , reset read as '0' 85 c 25 c -40 c 0 0.5 1 1.5 2 2.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) threshold (v) reset pin as io, input hysteresis vs. vcc v il , i/o pin read as "0" 85 c 25 c -40 c 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) input hysteresis (mv)
144 8126d?avr?11/09 attiny13a figure 19-39. reset input threshold voltage vs. v cc (v ih , reset pin read as '1') figure 19-40. reset input threshold voltage vs. v cc (v il , reset pin read as '0') reset input threshold voltage vs. vcc v ih , i/o pin read as '1' 85 c 25 c -40 c 0 0.5 1 1.5 2 2.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) threshold (v) reset input threshold voltage vs. vcc v il , i/o pin read as '0' 85 c 25 c -40 c 0 0.5 1 1.5 2 2.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) threshold (v)
145 8126d?avr?11/09 attiny13a figure 19-41. reset input pin hysteresis vs. v cc 19.8 bod thresholds figure 19-42. bod thresholds vs. temper ature (bodlevel is 4.3v) reset pin input hysteresis vs. v cc 85 c 25 c -40 c 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) input hysteresis (v) bod thresholds vs. temperature bodlevel = 4.3v v cc rising v cc falling 4.2 4.25 4.3 4.35 4.4 -60 -40 -20 0 20 40 60 80 100 temperature (c) threshold (v)
146 8126d?avr?11/09 attiny13a figure 19-43. bod thresholds vs. temper ature (bodlevel is 2.7v) figure 19-44. bod thresholds vs. temper ature (bodlevel is 1.8v) v cc rising v cc falling bod thresholds vs. temperature bodlevel = 2.7v 2.6 2.65 2.7 2.75 2.8 -60 -40 -20 0 20 40 60 80 100 temperature (c) threshold (v) v cc rising v cc falling bod thresholds vs. temperature bodlevel = 1.8v 1.7 1.75 1.8 1.85 1.9 -60 -40 -20 0 20 40 60 80 100 temperature (c) threshold (v)
147 8126d?avr?11/09 attiny13a figure 19-45. bandgap voltage vs. v cc 19.9 internal oscillator speed figure 19-46. calibrated 9.6 mhz rc oscillato r frequency vs. temperature bandgap voltage vs. v cc 85 c 25 c -40 c 1.06 1.08 1.1 1.12 1.14 1.5 2 2.5 3 3.5 4 4.5 5 5.5 vcc (v) bandgap voltage (v) calibrated 9.6mhz rc oscillator frequency vs. temperature 5.5 v 4.5 v 2.7 v 1.8 v 9 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10 -40 -20 0 20 40 60 80 100 temperature (c) frequency (mhz)
148 8126d?avr?11/09 attiny13a figure 19-47. calibrated 9.6 mhz rc osc illator frequency vs. v cc figure 19-48. calibrated 9.6 mhz rc oscillato r frequency vs. osccal value calibrated 9.6mhz rc oscillator frequency vs. operating voltage 85 c 25 c -40 c 9 9.2 9.4 9.6 9.8 10 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) frequency (mhz) calibrated 9.6mhz rc oscillator frequency vs. osccal value v cc = 3v 25 c 0 2 4 6 8 10 12 14 16 18 20 0 163248648096112 osccal frequency (mhz)
149 8126d?avr?11/09 attiny13a figure 19-49. calibrated 4.8 mhz rc oscillato r frequency vs. temperature figure 19-50. calibrated 4.8 mhz rc osc illator frequency vs. v cc calibrated 4.8mhz rc oscillator frequency vs. temperature 5.5 v 4.0 v 2.7 v 1.8 v 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5 -60 -40 -20 0 20 40 60 80 100 temperature frequency (mhz) calibrated 4.8mhz rc oscillator frequency vs. operating voltage 85 c 25 c -40 c 4.2 4.4 4.6 4.8 5 5.2 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) frequency (mhz)
150 8126d?avr?11/09 attiny13a figure 19-51. calibrated 4.8 mhz rc oscillato r frequency vs. osccal value figure 19-52. 128 khz watchdog osc illator frequency vs. v cc calibrated 4.8mhz rc oscillator frequency vs. osccal value v cc = 3v 25 c 0 1 2 3 4 5 6 7 8 9 10 0 163248648096112 osccal frequency (mhz) watchdog oscillator frequency vs. operating voltage 85 c 25 c -40 c 100000 102000 104000 106000 108000 110000 112000 114000 116000 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) frequency (hz)
151 8126d?avr?11/09 attiny13a figure 19-53. 128 khz watchdog oscillator frequency vs. temperature 19.10 current consumpti on of peripheral units figure 19-54. brownout detector current vs. v cc watchdog oscillator frequency vs. temperature 5.5 v 4.0 v 2.7 v 1.8 v 105000 106000 107000 108000 109000 110000 111000 112000 113000 114000 115000 -60 -40 -20 0 20 40 60 80 100 temperature frequency (kh) brownout detector current vs. v cc 85 c 25 c -40 c 0 5 10 15 20 25 30 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ua)
152 8126d?avr?11/09 attiny13a figure 19-55. adc current vs. v cc figure 19-56. analog comparator current vs. v cc adc current vs. v cc f = 1.0 mhz -40 c 25 c 85 c 0 50 100 150 200 250 300 350 400 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ua) analog comparator current vs. v cc f = 1.0 mhz -40 c 25 c 85 c 0 10 20 30 40 50 60 70 80 90 100 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ua)
153 8126d?avr?11/09 attiny13a figure 19-57. programming current vs. v cc 19.11 current consumption in reset and reset pulse width figure 19-58. reset supply current vs. v cc (0.1 - 1.0 mhz, excluding current through the reset pull-up) programming current vs. v cc 85 c 25 c -40 c 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ua) reset supply current vs. v cc excluding current through the reset pullup 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 frequency (mhz) i cc (ma)
154 8126d?avr?11/09 attiny13a figure 19-59. reset supply current vs. v cc (1 - 20 mhz, excluding current through the reset pull-up) figure 19-60. reset pulse width vs. v cc reset supply current vs. v cc excluding current through the reset pullup 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0.2 0.4 0.6 0.8 1 1.2 1.4 0 2 4 6 8 101214161820 frequency (mhz) i cc (ma) minimum reset pulse width vs. v cc 85 c 25 c -40 c 0 500 1000 1500 2000 0123456 v cc (v) pulsewidth (ns)
155 8126d?avr?11/09 attiny13a 20. register summary address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 page 0x3f sreg i t h s v n z c page 9 0x3e reserved ? ? ? ? ? ? ? ? 0x3d spl sp[7:0] page 11 0x3c reserved ? ? ? ? ? ? ? ? 0x3b gimsk ? int0 pcie ? ? ? ? ? page 47 0x3a gifr ? intf0 pcif ? ? ? ? ? page 48 0x39 timsk0 ? ? ? ? ocie0b ocie0a toie0 ? page 75 0x38 tifr0 ? ? ? ? ocf0b ocf0a tov0 ? page 76 0x37 spmcsr ? ? ? ctpb rflb pgwrt pgers selfpr- page 98 0x36 ocr0a timer/counter ? output compare register a page 75 0x35 mcucr ?pudsesm1sm0 ? isc01 isc00 pages 33 , 47 , 57 0x34 mcusr ? ? ? ? wdrf borf extrf porf page 42 0x33 tccr0b foc0a foc0b ? ? wgm02 cs02 cs01 cs00 page 73 0x32 tcnt0 timer/counter (8-bit) page 74 0x31 osccal oscillator calibration register page 27 0x30 bodcr ? ? ? ? ? ? bods bodse page 33 0x2f tccr0a com0a1 com0a0 com0b1 com0b0 ? ?wgm01wgm00 page 70 0x2e dwdr dwdr[7:0] page 97 0x2d reserved ? 0x2c reserved ? 0x2b reserved ? 0x2a reserved ? 0x29 ocr0b timer/counter ? output compare register b page 75 0x28 gtccr tsm ? ? ? ? ? ? psr10 page 78 0x27 reserved ? 0x26 clkpr clkpce ? ? ? clkps3 clkps2 clkps1 clkps0 page 28 0x25 prr ? ? ? ? ? ? prtim0 pradc page 34 0x24 reserved ? 0x23 reserved ? 0x22 reserved ? 0x21 wdtcr wdtif wdtie wdp3 wdce wde wdp2 wdp1 wdp0 page 42 0x20 reserved ? 0x1f reserved ? 0x1e eearl ? ? eeprom address register page 20 0x1d eedr eeprom data register page 20 0x1c eecr ? ? eepm1 eepm0 eerie eempe eepe eere page 21 0x1b reserved ? 0x1a reserved ? 0x19 reserved ? 0x18 portb ? ? portb5 portb4 portb3 portb2 portb1 portb0 page 57 0x17 ddrb ? ? ddb5 ddb4 ddb3 ddb2 ddb1 ddb0 page 57 0x16 pinb ? ? pinb5 pinb4 pinb3 pinb2 pinb1 pinb0 page 58 0x15 pcmsk ? ? pcint5 pcint4 pcint3 pcint2 pcint1 pcint0 page 48 0x14 didr0 ? ? adc0d adc2d adc3d adc1d ain1d ain0d pages 81 , 95 0x13 reserved ? 0x12 reserved ? 0x11 reserved ? 0x10 reserved ? 0x0f reserved ? 0x0e reserved ? 0x0d reserved ? 0x0c reserved ? 0x0b reserved ? 0x0a reserved ? 0x09 reserved ? 0x08 acsr acd acbg aco aci acie ? acis1 acis0 page 80 0x07 admux ? refs0 adlar ? ? ? mux1 mux0 page 92 0x06 adcsra aden adsc adate adif adie adps2 adps1 adps0 page 93 0x05 adch adc data register high byte page 94 0x04 adcl adc data register low byte page 94 0x03 adcsrb ?acme ? ? ? adts2 adts1 adts0 pages 80 , 95 0x02 reserved ? 0x01 reserved ? 0x00 reserved ?
156 8126d?avr?11/09 attiny13a notes: 1. for compatibility with future devices, reserved bits shou ld be written to zero if accesse d. reserved i/o memory address es should never be written. 2. i/o registers within the address range 0x00 - 0x1f are directly bit-accessible using the sbi and cbi instructions. in these registers, the value of single bits can be checked by using the sbis and sbic inst ructions.ome of the status flags are cleared by writing a logical one to them. note that, unlike mo st other avrs, the cbi and sbi instructions will only operation the specified bit, and can therefore be used on registers cont aining such status flags. the cbi and sbi instructions work with registers 0x00 to 0x1f only.
157 8126d?avr?11/09 attiny13a 21. instruction set summary mnemonics operands description operation flags #clocks arithmetic and logic instructions add rd, rr add two registers rd rd + rr z,c,n,v,h 1 adc rd, rr add with carry two registers rd rd + rr + c z,c,n,v,h 1 adiw rdl,k add immediate to word rdh:rdl rdh:rdl + k z,c,n,v,s 2 sub rd, rr subtract two registers rd rd - rr z,c,n,v,h 1 subi rd, k subtract constant from register rd rd - k z,c,n,v,h 1 sbc rd, rr subtract with carry two registers rd rd - rr - c z,c,n,v,h 1 sbci rd, k subtract with carry constant from reg. rd rd - k - c z,c,n,v,h 1 sbiw rdl,k subtract immediate from word rdh:rdl rdh:rdl - k z,c,n,v,s 2 and rd, rr logical and registers rd rd ? rr z,n,v 1 andi rd, k logical and register and constant rd rd ? k z,n,v 1 or rd, rr logical or registers rd rd v rr z,n,v 1 ori rd, k logical or register and constant rd rd v k z,n,v 1 eor rd, rr exclusive or registers rd rd rr z,n,v 1 com rd one?s complement rd 0xff ? rd z,c,n,v 1 neg rd two?s complement rd 0x00 ? rd z,c,n,v,h 1 sbr rd,k set bit(s) in register rd rd v k z,n,v 1 cbr rd,k clear bit(s) in register rd rd ? (0xff - k) z,n,v 1 inc rd increment rd rd + 1 z,n,v 1 dec rd decrement rd rd ? 1 z,n,v 1 tst rd test for zero or minus rd rd ? rd z,n,v 1 clr rd clear register rd rd rd z,n,v 1 ser rd set register rd 0xff none 1 branch instructions rjmp k relative jump pc pc + k + 1 none 2 ijmp indirect jump to (z) pc z none 2 rcall k relative subroutine call pc pc + k + 1 none 3 icall indirect call to (z) pc znone3 ret subroutine return pc stack none 4 reti interrupt return pc stack i 4 cpse rd,rr compare, skip if equal if (rd = rr) pc pc + 2 or 3 none 1/2/3 cp rd,rr compare rd ? rr z, n,v,c,h 1 cpc rd,rr compare with carry rd ? rr ? c z, n,v,c,h 1 cpi rd,k compare register with immediate rd ? k z, n,v,c,h 1 sbrc rr, b skip if bit in register cleared if (rr(b)=0) pc pc + 2 or 3 none 1/2/3 sbrs rr, b skip if bit in register is set if (rr(b)=1) pc pc + 2 or 3 none 1/2/3 sbic p, b skip if bit in i/o register cleared if (p(b)=0) pc pc + 2 or 3 none 1/2/3 sbis p, b skip if bit in i/o register is set if (p(b)=1) pc pc + 2 or 3 none 1/2/3 brbs s, k branch if status flag set if (sreg(s) = 1) then pc pc+k + 1 none 1/2 brbc s, k branch if status flag cleared if (sreg(s) = 0) then pc pc+k + 1 none 1/2 breq k branch if equal if (z = 1) then pc pc + k + 1 none 1/2 brne k branch if not equal if (z = 0) then pc pc + k + 1 none 1/2 brcs k branch if carry set if (c = 1) then pc pc + k + 1 none 1/2 brcc k branch if carry cleared if (c = 0) then pc pc + k + 1 none 1/2 brsh k branch if same or higher if (c = 0) then pc pc + k + 1 none 1/2 brlo k branch if lower if (c = 1) then pc pc + k + 1 none 1/2 brmi k branch if minus if (n = 1) then pc pc + k + 1 none 1/2 brpl k branch if plus if (n = 0) then pc pc + k + 1 none 1/2 brge k branch if greater or equal, signed if (n v= 0) then pc pc + k + 1 none 1/2 brlt k branch if less than zero, signed if (n v= 1) then pc pc + k + 1 none 1/2 brhs k branch if half carry flag set if (h = 1) then pc pc + k + 1 none 1/2 brhc k branch if half carry flag cleared if (h = 0) then pc pc + k + 1 none 1/2 brts k branch if t flag set if (t = 1) then pc pc + k + 1 none 1/2 brtc k branch if t flag cleared if (t = 0) then pc pc + k + 1 none 1/2 brvs k branch if overflow flag is set if (v = 1) then pc pc + k + 1 none 1/2 brvc k branch if overflow flag is cleared if (v = 0) then pc pc + k + 1 none 1/2 brie k branch if interrupt enabled if ( i = 1) then pc pc + k + 1 none 1/2 brid k branch if interrupt disabled if ( i = 0) then pc pc + k + 1 none 1/2 bit and bit-test instructions sbi p,b set bit in i/o register i/o(p,b) 1none2 cbi p,b clear bit in i/o register i/o(p,b) 0none2 lsl rd logical shift left rd(n+1) rd(n), rd(0) 0 z,c,n,v 1 lsr rd logical shift right rd(n) rd(n+1), rd(7) 0 z,c,n,v 1 rol rd rotate left through carry rd(0) c,rd(n+1) rd(n),c rd(7) z,c,n,v 1
158 8126d?avr?11/09 attiny13a ror rd rotate right through carry rd(7) c,rd(n) rd(n+1),c rd(0) z,c,n,v 1 asr rd arithmetic shift right rd(n) rd(n+1), n=0..6 z,c,n,v 1 swap rd swap nibbles rd(3..0) rd(7..4),rd(7..4) rd(3..0) none 1 bset s flag set sreg(s) 1 sreg(s) 1 bclr s flag clear sreg(s) 0 sreg(s) 1 bst rr, b bit store from register to t t rr(b) t 1 bld rd, b bit load from t to register rd(b) tnone1 sec set carry c 1c1 clc clear carry c 0 c 1 sen set negative flag n 1n1 cln clear negative flag n 0 n 1 sez set zero flag z 1z1 clz clear zero flag z 0 z 1 sei global interrupt enable i 1i1 cli global interrupt disable i 0 i 1 ses set signed test flag s 1s1 cls clear signed test flag s 0 s 1 sev set twos complement overflow. v 1v1 clv clear twos complement overflow v 0 v 1 set set t in sreg t 1t1 clt clear t in sreg t 0 t 1 seh set half carry flag in sreg h 1h1 clh clear half carry flag in sreg h 0 h 1 data transfer instructions mov rd, rr move between registers rd rr none 1 movw rd, rr copy register word rd+1:rd rr+1:rr none 1 ldi rd, k load immediate rd knone1 ld rd, x load indirect rd (x) none 2 ld rd, x+ load indirect and post-inc. rd (x), x x + 1 none 2 ld rd, - x load indirect and pre-dec. x x - 1, rd (x) none 2 ld rd, y load indirect rd (y) none 2 ld rd, y+ load indirect and post-inc. rd (y), y y + 1 none 2 ld rd, - y load indirect and pre-dec. y y - 1, rd (y) none 2 ldd rd,y+q load indirect with displacement rd (y + q) none 2 ld rd, z load indirect rd (z) none 2 ld rd, z+ load indirect and post-inc. rd (z), z z+1 none 2 ld rd, -z load indirect and pre-dec. z z - 1, rd (z) none 2 ldd rd, z+q load indirect with displacement rd (z + q) none 2 lds rd, k load direct from sram rd (k) none 2 st x, rr store indirect (x) rr none 2 st x+, rr store indirect and post-inc. (x) rr, x x + 1 none 2 st - x, rr store indirect and pre-dec. x x - 1, (x) rr none 2 st y, rr store indirect (y) rr none 2 st y+, rr store indirect and post-inc. (y) rr, y y + 1 none 2 st - y, rr store indirect and pre-dec. y y - 1, (y) rr none 2 std y+q,rr store indirect with displacement (y + q) rr none 2 st z, rr store indirect (z) rr none 2 st z+, rr store indirect and post-inc. (z) rr, z z + 1 none 2 st -z, rr store indirect and pre-dec. z z - 1, (z) rr none 2 std z+q,rr store indirect with displacement (z + q) rr none 2 sts k, rr store direct to sram (k) rr none 2 lpm load program memory r0 (z) none 3 lpm rd, z load program memory rd (z) none 3 lpm rd, z+ load program memory and post-inc rd (z), z z+1 none 3 spm store program memory (z) r1:r0 none in rd, p in port rd pnone1 out p, rr out port p rr none 1 push rr push register on stack stack rr none 2 pop rd pop register from stack rd stack none 2 mcu control instructions nop no operation none 1 sleep sleep (see specific descr. for sleep function) none 1 wdr watchdog reset (see specific descr. for wdr/timer) none 1 break break for on-chip debug only none n/a mnemonics operands description operation flags #clocks
159 8126d?avr?11/09 attiny13a 22. ordering information notes: 1. for device speed vs. v cc , see ?speed grades? on page 118 . 2. this device can also be supplied in wafer form. please contact your local atmel sales office for detailed ordering informatio n and minimum quantities. 3. all packages are pb-free, halide-free, fu lly green and they comply with the european directive for restriction of hazardous substances (rohs). 4. nipdau finish. 5. topside marking for attiny13a: ? 1st line: t13 ? 2nd line: axx ? 3rd line: xxx speed (mhz) (1) power supply (v) (1) ordering code package (2)(3) operation range 20 1.8 - 5.5 ATTINY13A-PU attiny13a-su attiny13a-sh (4) attiny13a-ssu attiny13a-ssh (4) attiny13a-mu attiny13a-mmu (5) 8p3 8s2 8s2 8s1 8s1 20m1 10m1 (5) industrial (-40 ? c to 85 ? c) package type 8p3 8-lead, 0.300" wide, plastic dual inline package (pdip) 8s2 8-lead, 0.209" wide, plastic small outline package (eiaj soic) 8s1 8-lead, 0.150" wide, plastic gull-wing small outline (jedec soic) 20m1 20-pad, 4 x 4 x 0.8 mm body, lead pitch 0.50 mm, micro lead frame package (mlf) 10m1 10-pad, 3 x 3 x 1 mm body, lead pitch 0.50 mm, micro lead frame package (mlf)
160 8126d?avr?11/09 attiny13a 23. packaging information 23.1 8p3 2325 orchard parkway san jose, ca 95131 title drawing no. r rev. 8p3 , 8-lead, 0.300" wide body, plastic dual in-line package (pdip) 01/09/02 8p3 b d d1 e e1 e l b2 b a2 a 1 n ea c b3 4 plcs top view side view end view common dimensions (unit of measure = inches) symbol min nom max note notes: 1. this drawing is for general information only; refer to jedec drawing ms-001, variation ba for additional information. 2. dimensions a and l are measured with the package seated in jedec seating plane gauge gs-3. 3. d, d1 and e1 dimensions do not include mold flash or protrusions. mold flash or protrusions shall not exceed 0.010 inch. 4. e and ea measured with the leads constrained to be perpendicular to datum. 5. pointed or rounded lead tips are preferred to ease insertion. 6. b2 and b3 maximum dimensions do not include dambar protrusions. dambar protrusions shall not exceed 0.010 (0.25 mm). a 0.210 2 a2 0.115 0.130 0.195 b 0.014 0.018 0.022 5 b2 0.045 0.060 0.070 6 b3 0.030 0.039 0.045 6 c 0.008 0.010 0.014 d 0.355 0.365 0.400 3 d1 0.005 3 e 0.300 0.310 0.325 4 e1 0.240 0.250 0.280 3 e 0.100 bsc ea 0.300 bsc 4 l 0.115 0.130 0.150 2
161 8126d?avr?11/09 attiny13a 23.2 8s2 title drawing no. gpc rev. packa g e drawin g contact: packagedrawings@atmel.com 8s2 stn f 8s2, 8-lead, 0.208? body, plastic small outline package (eiaj) 4/15/08 common dimensions (unit of measure = mm) symbol min nom max note notes: 1. this drawing is for general information only; refer to eiaj drawing edr-7320 for additional information. 2. mismatch of the upper and lower dies and resin burrs aren't included. 3. determines the true geometric position. 4. values b,c apply to plated terminal. the standard thickness of the plating layer shall measure between 0.007 to .021 mm. a 1.70 2.16 a1 0.05 0.25 b 0.35 0.48 4 c 0.15 0.35 4 d 5.13 5.35 e1 5.18 5.40 2 e 7.70 8.26 l 0.51 0.85 0 8 e 1.27 bsc 3 1 1 n n e e top view t o p v i e w c c e1 e 1 end view e n d v i e w a a b b l l a1 a 1 e e d d side view s i d e v i e w
162 8126d?avr?11/09 attiny13a 23.3 8s1 2325 orchard parkway san jose, ca 95131 title drawing no. r rev. note: 10/10/01 8s1 , 8-lead (0.150" wide body), plastic gull wing small outline (jedec soic) 8s1 a h 1 2 n 3 top view c e end view a b l a2 e d side view common dimensions (unit of measure = mm) symbol min nom max note this drawing is for general information only. refer to jedec drawing ms-012 for proper dimensions, tolerances, datums, etc. a ? ? 1.75 b ? ? 0.51 c ? ? 0.25 d ? ? 5.00 e ? ? 4.00 e 1.27 bsc h ? ? 6.20 l ? ? 1.27
163 8126d?avr?11/09 attiny13a 23.4 20m1 2325 orchard parkway san jose, ca 95131 title drawing no. r rev. 20m1 , 20-pad, 4 x 4 x 0.8 mm body, lead pitch 0.50 mm, a 20m1 10/27/04 2.6 mm exposed pad, micro lead frame package (mlf) a 0.70 0.75 0.80 a1 ? 0.01 0.05 a2 0.20 ref b 0.18 0.23 0.30 d 4.00 bsc d2 2.45 2.60 2.75 e 4.00 bsc e2 2.45 2.60 2.75 e 0.50 bsc l 0.35 0.40 0.55 side view pin 1 id pin #1 notch (0.20 r) bottom view top view note: reference jedec standard mo-220, fig . 1 (saw singulation) wggd-5. common dimensions (unit of measure = mm) symbol min nom max note d e e a2 a1 a d2 e2 0.08 c l 1 2 3 b 1 2 3
164 8126d?avr?11/09 attiny13a 23.5 10m1 2325 orchard parkway san jose, ca 95131 title drawing no. r rev. 10m1, 10-pad, 3 x 3 x 1.0 mm body, lead pitch 0.50 mm, 1.64 x 2.60 mm exposed pad, micro lead frame package a 10m1 7/7/06 common dimensions (unit of measure = mm) symbol min nom max note a 0.80 0.90 1.00 a1 0.00 0.02 0.05 b 0.18 0.25 0.30 d 2.90 3.00 3.10 d1 1.40 ? 1.75 e 2.90 3.00 3.10 e1 2.20 ? 2.70 e 0.50 l 0.30 ? 0.50 y ? ? 0.08 k 0.20 ? ? pin 1 id top view d e a1 a side view bottom view d1 e1 l b e k 1 2 notes: 1. this package conforms to jedec reference mo-229c, variation veed-5. 2. the terminal #1 id is a lasser-marked feature. y
165 8126d?avr?11/09 attiny13a 24. errata the revision letters in this section refe r to the revision of the attiny13a device. 24.1 attiny13a rev. g ? h ? eeprom can not be wr itten below 1.9 volt 1. eeprom can not be written below 1.9 volt writing the eeprom at v cc below 1.9 volts might fail. problem fix/workaround do not write the eeprom when v cc is below 1.9 volts. 24.2 attiny13a rev. e ? f these device revisions were not sampled. 24.3 attiny13a rev. a ? d these device revisions were referred to as attiny13/attiny13v.
166 8126d?avr?11/09 attiny13a 25. datasheet revision history please note that page numbers in this section re fer to the current version of this document and may not apply to previous versions. 25.1 rev. 8126d ? 11/09 1. added note ?if the rstdispl fuse is programmed..? in startup-up times table 6-5 and table 6-6 on page 26 . 2. added addresses in all register description tables and cross-references to register summary. 3. updated naming convention for -com bits in tables from table 11-2 on page 70 to table 11-7 on page 72 . 4. updated value for t wd_erase in table 17-8, ?minimum wait de lay before writing the next flash or eeprom loc ation,? on page 108 . 5. added nipdau note for -sh and -ssh in section 22. ?ordering information? on page 159 . 25.2 rev. 8126c ? 09/09 1. added eeprom errata for rev. g - h on page 165 . 2. added a note about topside marking in section 22. ?ordering information? on page 159 . 25.3 rev. 8126b ? 11/08 1. updated order codes on page 159 to reflect changes in material composition. 2. updated sections: ? ?didr0 ? digital input disable register 0? on page 81 ? ?didr0 ? digital input disable register 0? on page 95 3. updated ?register summary? on page 155 . 25.4 rev. 8126a ? 05/08 1. initial revision, created from document 2535i ? 04/08. 2. updated characterist ic plots of section ?typical characteristics? , starting on page 124 . 3. updated ?ordering information? on page 159 . 4. updated section: ? ?speed grades? on page 118 5. update tables: ? ?dc characteristics, ta = -40c to 85c? on page 117 ? ?calibration accuracy of intern al rc oscillator? on page 119 ? ?reset, brown-out, and internal voltage characteristics? on page 120 ? ?adc characteristics, single ended channels. ta = -40c - 85c? on page 121 ? ?serial programming characteristics, ta = -40c to 85c? on page 122 6. added description of new function, ?power reduction register?: ? added functional description on page 31 ? added bit description on page 34 ? added section ?supply current of i/o modules? on page 124 ? updated register summary on page 155
167 8126d?avr?11/09 attiny13a 7. added description of new function, ?software bod disable?: ? added functional description on page 31 ? updated section on page 32 ? added register description on page 33 ? updated register summary on page 155 8. added description of enhanced function, ?enhanced power-on reset?: ? updated table 18-4 on page 120 , and table 18-5 on page 120
168 8126d?avr?11/09 attiny13a
i 8126d?avr?11/09 attiny13a table of contents features .............. ................ ................ ............... .............. .............. ............ 1 1 pin configurations ... ................ ................ ................. ................ ............... 2 1.1 pin description ..................................................................................................3 2 overview ................ .............. .............. ............... .............. .............. ............ 4 2.1 block diagram ...................................................................................................4 3 about ............. ................ ................. ................ ................. .............. ............ 6 3.1 resources .........................................................................................................6 3.2 code examples .................................................................................................6 3.3 data retention ...................................................................................................6 4cpu core ............... .............. .............. ............... .............. .............. ............ 7 4.1 architectural overview .......................................................................................7 4.2 alu ? arithmetic logic unit ...............................................................................8 4.3 status register ..................................................................................................8 4.4 general purpose register file ........................................................................10 4.5 stack pointer ...................................................................................................11 4.6 instruction execution timing ...........................................................................12 4.7 reset and interrupt handling ...........................................................................12 5 memories ............... .............. .............. ............... .............. .............. .......... 15 5.1 in-system reprogrammabl e flash program memory .....................................15 5.2 sram data memory ........................................................................................15 5.3 eeprom data memory . ................. ................ ............. ............ ............. ..........16 5.4 i/o memory ......................................................................................................20 5.5 register description ........................................................................................20 6 system clock and clock opti ons ........... ................. ................ ............. 23 6.1 clock systems and their distribution ...............................................................23 6.2 clock sources .................................................................................................24 6.3 system clock prescaler ..................................................................................26 6.4 register description ........................................................................................27 7 power management and sleep modes ........... .............. .............. .......... 30 7.1 sleep modes ....................................................................................................30 7.2 software bod disable .....................................................................................31 7.3 power reduction register ...............................................................................31
ii 8126d?avr?11/09 attiny13a 7.4 minimizing power consumption ......................................................................32 7.5 register description ........................................................................................33 8 system control and reset .... .............. .............. .............. .............. ........ 35 8.1 resetting the avr ...........................................................................................35 8.2 reset sources .................................................................................................36 8.3 internal voltage reference ..............................................................................38 8.4 watchdog timer ..............................................................................................38 8.5 register description ........................................................................................42 9 interrupts ............... .............. .............. ............... .............. .............. .......... 45 9.1 interrupt vectors ..............................................................................................45 9.2 external interrupts ...........................................................................................46 9.3 register description ........................................................................................47 10 i/o ports ............... ................ .............. ............... .............. .............. .......... 49 10.1 overview ..........................................................................................................49 10.2 ports as general digital i/o .............................................................................50 10.3 alternate port functions ..................................................................................54 10.4 register description ........................................................................................57 11 8-bit timer/counter0 with pw m .............. ................. ................ ............. 59 11.1 features ..........................................................................................................59 11.2 overview ..........................................................................................................59 11.3 timer/counter clock sources .........................................................................60 11.4 counter unit ....................................................................................................60 11.5 output compare unit .......................................................................................61 11.6 compare match output unit ............................................................................63 11.7 modes of operation .........................................................................................64 11.8 timer/counter timing diagrams ........... ..........................................................68 11.9 register description ........................................................................................70 12 timer/counter prescaler ....... .............. .............. .............. .............. ........ 77 12.1 overview ..........................................................................................................77 12.2 prescaler reset ...............................................................................................77 12.3 external clock source .....................................................................................77 12.4 register description. .......................................................................................78 13 analog comparator ............ .............. ............... .............. .............. .......... 79 13.1 analog comparator multiplexed input .............................................................79
iii 8126d?avr?11/09 attiny13a 13.2 register description ........................................................................................80 14 analog to digital converter ............. ............... .............. .............. .......... 82 14.1 features ..........................................................................................................82 14.2 overview ..........................................................................................................82 14.3 operation .........................................................................................................83 14.4 starting a conversion ......................................................................................83 14.5 prescaling and conversion timing ..................................................................84 14.6 changing channel or reference selection .....................................................87 14.7 adc noise canceler .......................................................................................88 14.8 analog input circuitry ......................................................................................88 14.9 analog noise canceling techniques ...............................................................89 14.10 adc accuracy definitions ...............................................................................89 14.11 adc conversion result ...................................................................................92 14.12 register description ........................................................................................92 15 debugwire on-chip debug s ystem .............. .............. .............. .......... 96 15.1 features ..........................................................................................................96 15.2 overview ..........................................................................................................96 15.3 physical interface ............................................................................................96 15.4 software break points .....................................................................................97 15.5 limitations of debugwire ...............................................................................97 15.6 register description ........................................................................................97 16 self-programming the flash ................. .............. .............. ............ ........ 98 16.1 performing page erase by spm ......................................................................98 16.2 filling the temporary buffer (page loading) ...................................................98 16.3 performing a page write .................... .............................................................99 16.4 addressing the flash during self-pro gramming .............................................99 16.5 eeprom write prevents writing to spmcsr ............. ............ ............. ........100 16.6 reading fuse and lock bits from firm ware .................................................100 16.7 preventing flash corruption ..........................................................................101 16.8 programming time for flash when using spm ............................................101 16.9 register description ......................................................................................102 17 memory programming ........ .............. ............... .............. .............. ........ 103 17.1 program and data memory lock bits ...........................................................103 17.2 fuse bytes .....................................................................................................104 17.3 calibration bytes ...........................................................................................105
iv 8126d?avr?11/09 attiny13a 17.4 signature bytes .............................................................................................105 17.5 page size ......................................................................................................105 17.6 serial programming ..... ..................................................................................106 17.7 high-voltage serial programming ...... ...........................................................109 17.8 considerations for efficient programming .....................................................113 18 electrical characteristics ... .............. ............... .............. .............. ........ 117 18.1 absolute maximum rating s* .........................................................................117 18.2 dc characteristics .........................................................................................117 18.3 speed grades ...............................................................................................118 18.4 clock characteristics .....................................................................................119 18.5 system and reset characteristics ................................................................120 18.6 analog comparator characteristics ...............................................................121 18.7 adc characteristics ......................................................................................121 18.8 serial programming characteristics ... ...........................................................122 18.9 high-voltage serial programming characteristics .........................................123 19 typical characteristics ..... .............. .............. .............. .............. ........... 124 19.1 supply current of i/o modules ......................................................................124 19.2 active supply current ....................................................................................125 19.3 idle supply current ........................................................................................128 19.4 power-down supply current .........................................................................131 19.5 pin pull-up .....................................................................................................132 19.6 pin driver strength ........................................................................................134 19.7 pin thresholds and hysteresis ......................................................................141 19.8 bod thresholds ............................................................................................145 19.9 internal oscillator speed ...............................................................................147 19.10 current consumption of peripheral units ......................................................151 19.11 current consumption in reset and re set pulse width .................................153 20 register summary ............ .............. .............. .............. .............. ........... 155 21 instruction set summary ... .............. ............... .............. .............. ........ 157 22 ordering information .......... .............. ............... .............. .............. ........ 159 23 packaging information ....... .............. ............... .............. .............. ........ 160 23.1 8p3 ................................................................................................................160 23.2 8s2 ................................................................................................................161 23.3 8s1 ................................................................................................................162
v 8126d?avr?11/09 attiny13a 23.4 20m1 ..............................................................................................................163 23.5 10m1 ..............................................................................................................164 24 errata ........... ................ ................ ................. ................ .............. ........... 165 24.1 attiny13a rev. g ? h ...................................................................................165 24.2 attiny13a rev. e ? f ....................................................................................165 24.3 attiny13a rev. a ? d ....................................................................................165 25 datasheet revision history .. ................ ................. ................ ............. 166 25.1 rev. 8126d ? 11/09 .......................................................................................166 25.2 rev. 8126c ? 09/09 .......................................................................................166 25.3 rev. 8126b ? 11/08 .......................................................................................166 25.4 rev. 8126a ? 05/08 .......................................................................................166 table of contents............... ................ ................. .............. .............. ........... i
8126d?avr?11/09 ? 2009 atmel corporation. all rights reserved. atmel ? , logo and combinations thereof, avr ? and others are registered trademarks or trade- marks of atmel corporation or its subsidiaries. other terms and product names may be trademarks of others. headquarters international atmel corporation 2325 orchard parkway san jose, ca 95131 usa tel: 1(408) 441-0311 fax: 1(408) 487-2600 atmel asia unit 1-5 & 16, 19/f bea tower, millennium city 5 418 kwun tong road kwun tong, kowloon hong kong tel: (852) 2245-6100 fax: (852) 2722-1369 atmel europe le krebs 8, rue jean-pierre timbaud bp 309 78054 saint-quentin-en- yvelines cedex france tel: (33) 1-30-60-70-00 fax: (33) 1-30-60-71-11 atmel japan 9f, tonetsu shinkawa bldg. 1-24-8 shinkawa chuo-ku, tokyo 104-0033 japan tel: (81) 3-3523-3551 fax: (81) 3-3523-7581 product contact web site www.atmel.com technical support avr@atmel.com sales contact www.atmel.com/contacts literature requests www.atmel.com/literature disclaimer: the information in this document is provided in connection wi th atmel products. no license, expr ess or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of atmel products. except as set forth in atmel?s terms and condi- tions of sale located on atmel?s web site, atmel assumes no li ability whatsoever and disclaims any express, implied or statutor y warranty relating to its products including , but not limited to, the implied warranty of merchantability, fitness for a particu lar purpose, or non-infringement. in no event shall atmel be liable for any direct, indire ct, consequential, punitive, special or i nciden- tal damages (including, without li mitation, damages for loss of profits, business interruption, or loss of information) arising out of the use or inability to use this document, even if atmel has been advised of the possibility of such damages. atmel makes no representations or warranties with respect to the accuracy or comp leteness of the contents of th is document and reserves the rig ht to make changes to specifications and product descriptions at any time without notice. atmel does not make any commitment to update the information contained her ein. unless specifically provided otherwise, atmel products are not suitable for, and shall not be used in, automotive applications. atmel?s products are not int ended, authorized, or warranted for use as components in applications intended to support or sustain life.


▲Up To Search▲   

 
Price & Availability of ATTINY13A-PU

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X